About Question enthuware.oce-jpad.v6.2.518 :

Moderator: admin

Post Reply

About Question enthuware.oce-jpad.v6.2.518 :

Post by cosminvacaroiu »

Isn't a

Code: Select all

required here ? otherwise the persist will do nothing

Site Admin
Posts: 9119
Joined: Fri Sep 10, 2010 9:26 pm

Re: About Question enthuware.oce-jpad.v6.2.518 :

Post by admin »

No, it is not required here because a new transaction is being created in the same code. joinTransaction is required when the transaction is created else where.

If you like our products and services, please help us by posting your review here.

Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm

Re: About Question enthuware.oce-jpad.v6.2.518 :

Post by himaiMinh »

I have the same question too. The entity manager is not created before em.getTransaction().begin which creates the transaction.
Why we don't need to call em.joinTransaction?
I assume the code is like this:

Code: Select all

EntityManagerFactory emf = Persistence.createEntityManagerFactory("aPU");
  EntityManager em = emf.createEntityManager();

  //This persistence context is created when the em is created.
  // It is created outside the new transaction 

   //The new transaction is created and begin after the em is created.

The JPA Pro says "If the persistence context was created earlier(outside of a transaction or in a transaction that has since ended), the persistence context can be manually synchronized with the transaction by calling joinTransaction() on the EntityManager interface."
I guess the above statement is only applicable to the application managed entity manager used with JTA.
For example:

Code: Select all

//In a stateful  bean
EntityManagerFactory emf;
EntityManager em;
public void init(){
 emf = Persistence.createEntityManagerFactory("aPU");
 em = emf.createEntityManager();

public void add(....){
   //anEntity is an entity

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests