Does not recognize my createQuery method

1

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.

    
asked by anonymous 28.05.2015 / 00:13

1 answer

0

You're trying to use createQuery correctly, but on the wrong object.

To fix it, do the following:

EntityManager em = manager.createEntityManager();
categoriasRaizes = em.createQuery("from Categoria",
        Categoria.class).getResultList();

In any case, I recommend a getEntityManager method for your class EntityManagerProducer , getting something like:

private EntityManager em;

public EntityManager getEntityManager(){
    if(this.em == null) {
        this.em = createEntityManager(); 
    }
    return this.em;
}

In this way you guarantee the reduction of overhead in the consumption / creation of the entityManagers (Paleativamente, I suggest later improvements).

    
30.01.2016 / 04:02