• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Usage of EntityManagerFactory

 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Devaka Cooray
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
application manage persistence context can be shared on multiple transaction since life of container managed persistence context life cycle limited to one transaction,
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic