1

I have a simple question to ask regarding JPA's CriteriaQuery-based queries? I noticed in the Java EE examples (http://docs.oracle.com/javaee/6/tutorial/doc/giqsq.html) that

entityManager.getTransaction().begin()

and

entityManager.getTransaction().commit()

are not used.

As a result, is it true that these operations are implicitly handled by the CriteriaQuery when the query is made?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Some Newbie
  • 1,059
  • 3
  • 14
  • 33

2 Answers2

3

If you are using JPA only without EJB, then you need to control transaction as yourself which means write transation().begin(),commit(),etc. But you are using JPA with EJB like as oracle's tutorial, you don't need to write transation().begin(), commit(), etc in your methods.

Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65
  • 1
    Interesting. How do I know if I am using EJB? I am using JPA from the Netbeans J2EE package. Not sure if that's a default. – Some Newbie Aug 15 '12 at 14:02
3

Criteria does not do anything about transactions, and neither does a normal JPQL query, or a named query. The transaction is controlled separately, and you can perform operations (such as a query) as transactional, or nontransactional. Obviously Java EE will enable transactions for you in general, whereas in Java SE you have to put them yourself - but that is separate from whichever query mechanism you use.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
DataNucleus
  • 15,497
  • 3
  • 32
  • 37