The find () method of my DAO Generic entityManager is giving NullPointer, how to fix it?

1

The change from my DAO looks like this:

private EntityManager entityManager;

public void alterar() throws Exception {
    System.out.println("T encontrada");
    System.out.println("iniciando Alterar id: " + this.getId());
    System.out.println("this Class" + this.getClass());
    T encontrada = (T) entityManager.find(this.getClass(), this.getId());
    System.out.println("encontrada");
    try {
        System.out.println("Iniciando a transacao de alteracao");
        EntityManagerControl.transactionBegin(entityManager);
        entityManager.merge(this);
        EntityManagerControl.transactionCommit(entityManager);
    } catch (Exception e) {
        EntityManagerControl.transactionRollback(entityManager);
        e.printStackTrace();
    } finally {
        // manager.close();
    }
}

I have records in the database and I'm with that record recorded in the bank but the moment I declare in the Controller object.change (), it's nullPointer in the find following command:

T encontrada = (T) entityManager.find(this.getClass(), this.getId());

How do you fix this error?

    
asked by anonymous 20.08.2015 / 22:20

1 answer

1

You must inject the EntityManager. An easier way would be with the help of VRaptor itself with the @Inject annotation

public class ProdutoDao {

    @Inject
    private EntityManager manager;

    public void adiciona(Produto produto) {
        manager.persist(produto);
    }
    //...
}

And take somewhere else @Produces or use the JPA plugin as can be seen in more detail at documentation .

    
03.09.2015 / 15:27