Why can’t I give getTransaction(). Begin() were from a Main?

Asked

Viewed 51 times

1

I’m playing with Hibernate and I came up with a very amateur question.

Before I was creating a DAO, and in it I received an Entitymanager from my test class, by the constructor:

public class ExemploDao {

private EntityManager em;

public ExemploDao(EntityManager em) {
    this.em=em;
}

Called in the Test Class:

EntityManager em = new JPAUtil().getEntityManager();
em.getTransaction().begin();

ExemploDao dao = new ExemploDao(em);

After that I went to do a test creating a EntityManager and calling the getTransaction().begin() in my DAO, instead of having to raise it in the test class:

    public class ExemploMovimentacaoDao {


    EntityManager em = new JPAUtil().getEntityManager();
    em.getTransaction().begin();

    public List<Double> getMediasPorDiaETipo(TipoMovimentacao saida, Conta conta) {

    String jpql = "select avg(m.valor) from Movimentacao m where m.conta= :pConta" 
    + " and m.tipo= :pTipo" 
    +" group by day(m.dataMovimentacao),month(m.dataMovimentacao),year(m.dataMovimentacao)";

    TypedQuery<Double> query = em.createQuery(jpql,Double.class);
    query.setParameter("pConta", conta);
    query.setParameter("pTipo", saida);


    return query.getResultList();


    em.close();
}


}

The following error occurred on the line of getTransaction:

Syntax error on token getTransaction, identifier expected after this token.

If I put it inside a Main the error disappears. As a beginner, I have had similar problems, where I could not call certain things out of a main method and never really figured out why. So I ask for your help, can you explain to me why this happens? If you have links of materials on the Internet that talk about it, I couldn’t find anything that would take away this very basic doubt, but I need to understand.

Thank you.

  • please update your question with the full sample class you are trying to give gettransaction to understand better

  • Updated question with class, thank you.

1 answer

0


You are stumbling on a java syntactic rule, it is not possible to make this kind of statement out of a method. A simple fix would be:

public class ExemploMovimentacaoDao {


    EntityManager em = new JPAUtil().getEntityManager();


    public List<Double> getMediasPorDiaETipo(TipoMovimentacao saida, Conta conta) {
    em.getTransaction().begin(); //estou dentro de um método agora!
    String jpql = "select avg(m.valor) from Movimentacao m where m.conta= :pConta" 
    + " and m.tipo= :pTipo" 
    +" group by day(m.dataMovimentacao),month(m.dataMovimentacao),year(m.dataMovimentacao)";

    TypedQuery<Double> query = 
    em.createQuery(jpql,Double.class);
    query.setParameter("pConta", conta);
    query.setParameter("pTipo", saida);


    return query.getResultList();


    em.close(); //ps: você nunca vai cair aqui devido ao return acima
}


}
  • Thanks, I get it. Silly me, this kind of thing has to do inside a method.

Browser other questions tagged

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