I have a JSF2.2 project with CDI, and I configured my project correctly for CDI, which is very strange is that the createQuery method is not being recognized, other times it did the same way of programming and I never had a problem , but now I'm having difficulty.
See how you are figuring out
isreferringtothissnippetofcode
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();
}
}
It was not for this problem.