1
I am with a JSF2.2 project with CDI, and I have correctly configured my project for CDI, what is very strange is that the createQuery method is not being recognized, other times did the same way of programming and never had problem, but now I’m having difficulty.
Look how it’s playing out
refers to that part of the code
categoriasRaizes = manager.createQuery("from Categoria",
Categoria.class).getResultList();
Now look at my Bean class
package com.algaworks.pedidovenda.controller;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.algaworks.pedidovenda.model.Categoria;
import com.algaworks.pedidovenda.model.Produto;
import com.algaworks.pedidovenda.util.jpa.EntityManagerProducer;
@Named
@ViewScoped
public class CadastroProdutoBean implements Serializable {
private static final long serialVersionUID = 1L;
private Produto produto;
private List<Categoria> categoriasRaizes;
@Inject
private EntityManagerProducer manager; //>>>>>> está sendo realizado injeção pelo CDI para fazer funcionar a o método inicializar
public CadastroProdutoBean() {
produto = new Produto();
}
public void inicializar() {
categoriasRaizes = manager.createQuery("from Categoria",
Categoria.class).getResultList();
}
public void salvar() {
}
public Produto getProduto() {
return produto;
}
public List<Categoria> getCategoriasRaizes() {
return categoriasRaizes;
}
}
This is the Entitymanagerproducer class
package com.algaworks.pedidovenda.util.jpa;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
@ApplicationScoped
public class EntityManagerProducer {
private EntityManagerFactory factory;
public EntityManagerProducer() {
factory = Persistence.createEntityManagerFactory("PedidoPU");
}
@Produces @RequestScoped
public EntityManager createEntityManager() {
return factory.createEntityManager();
}
public void closeEntityManager(@Disposes EntityManager manager) {
manager.close();
}
}
Wasn’t supposed to be giving this trouble.