This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Usage of EntityManagerFactory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Usage of EntityManagerFactory" Watch "Usage of EntityManagerFactory" New topic
Author

Usage of EntityManagerFactory

Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3060
    
  38

Can anyone tell me, what is the usage of using @PersistenceUnit annotation, instead of using @PersistenceContext annotation.

When we used @PersistenceUnit, we can get a EntityManagerFactory object as:


In this case, we can get the EntityManager as:


However, we can use the following syntax to get the EntityManager directly, with @PersistenceContext annotation as:


So, why we use the long way with @PersistenceUnit annotation, instead of using @PersistenceContext annotation?
[ November 03, 2008: Message edited by: Devaka Cooray ]

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The EntityManagerFactory, along with the @PersistenceUnit annotation is used to create an Application-managed Persistence Context. The main difference between an application-managed persistence context and a context-managed persistence context is that application-managed persistence contexts are extended in scope, and may span multiple transactions. Using a transaction-scoped container-managed persistence context (like in your last code) limits the scope of the persistence context to the existing transaction.


[My Blog]
All roads lead to JavaRanch
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3060
    
  38

Originally posted by Christophe Verre:
The EntityManagerFactory, along with the @PersistenceUnit annotation is used to create an Application-managed Persistence Context. The main difference between an application-managed persistence context and a context-managed persistence context is that application-managed persistence contexts are extended in scope, and may span multiple transactions. Using a transaction-scoped container-managed persistence context (like in your last code) limits the scope of the persistence context to the existing transaction.



Thanks for the quick reply. However I didn't understand this well. Can you explain me?
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402
application manage persistence context can be shared on multiple transaction since life of container managed persistence context life cycle limited to one transaction,
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi,
The main difference between an application-managed persistence context and a context-managed persistence context is that application-managed persistence contexts are extended in scope, and may span multiple transactions
Can you give an example where an extended persistence context is used in a reasonable way in a stateless session bean or a MDB ?

In stateful session beans it seems to be easier to aquire an extended persistence context via

@PersistenceContext(type=PersistenceContextType.EXTENDED)


SCJP 5 (98%) - SCBCD 5 (98%)
Afzal Hossain
Greenhorn

Joined: Jan 02, 2006
Posts: 25
Hi Friends,
When the PersistenceUnit is used it not only injects the EntityManagerFactory, it also registers a reference to it within the JNDI ENC of the EJB.

thanks


Afzal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Usage of EntityManagerFactory