Page 1 of 1

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

Posted: Sun Sep 02, 2012 8:48 am
by cosminvacaroiu
Isn't a

Code: Select all

em.joinTransaction()
required here ? otherwise the persist will do nothing

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

Posted: Sun Sep 09, 2012 3:19 pm
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.

HTH,
Paul.

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

Posted: Sat Aug 12, 2017 5:08 pm
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.
   em.getTransaction().begin();

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
@PersistenceContext(unitName="aPU")
EntityManagerFactory emf;
EntityManager em;
public void init(){
 emf = Persistence.createEntityManagerFactory("aPU");
 em = emf.createEntityManager();
}

public void add(....){
   em.joinTranscation();
   //anEntity is an entity
   em.persist(anEntity);
}