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
– Lucas Miranda
Updated question with class, thank you.
– Adriano