Does not recognize my createQuery method

Asked

Viewed 90 times

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

inserir a descrição da imagem aqui

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.

1 answer

0

Are you trying to utilize the createQuery in the right way, but in the wrong object.

To correct, do as follows:

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

Anyway, I advise a method getEntityManager for your class EntityManagerProducer, getting something like:

private EntityManager em;

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.