• 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

Difference in @PersistenceUnit and @PersistenseContext

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I am bit confused on the difference of @PersistenceUnit and @PersistenseContext. Anyone please explain.

Thanks
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@PersistenceContext injects an instance of EntityManager (used for managing entities)
@PersistenceUnit injects an instance of EntityManagerFactory (used for creating EntityManagers)

...quite confusing when you first encounter them.
They both share the unitName parameter attribute.. which identifies the persistence unit. You'd typically use EntityManagerFactory only when you intend to manage the life-cycle of your EntityManagers... but generally, you're better of just injecting a container managed EntityManager using the @PersistenceContext




[corrected error: Thanks Niranjan, that was a copy and paste error. fixed it now]
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bod,

A small correction. In the second code snippet it should be

entityManager.<Entity_management_methods>();

instead of

entityManager.createEntityManager();

To add on that, @PersistenceUnit is typically used for Java SE. Correct me if I am wrong!
 
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

To add on that, @PersistenceUnit is typically used for Java SE. Correct me if I am wrong!


In J2SE, who is going to inject the reference ? @PersistenceUnit can only be used in a J2EE environment. In J2SE, you have to use Persistence.createEntityManagerFactory instead.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I missed the Persistence.createEntityManager() method! Thanks!
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys

so in nutshell we can say that in case of @PersistenceUnit developer needs to worry about releasing the resources acquired by EntityManager and in case of @PersistenceContext container do this for us.
 
Bod Toki
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

so in nutshell we can say that in case of @PersistenceUnit developer needs to worry about releasing the resources acquired by EntityManager and in case of @PersistenceContext container do this for us.



yes.. basically that. and you can explicity join a container-managed JTA transaction like this:



EntityManagers you get via @PersistenceContext are typically called Container Managed Entity Manager because the container manages things for you.
while EntityManagers you get via @PersistenceUnit / entityManagerFactory.createEntityManager() are called Application Managed Entity Manager because you'd have to manage certain things in your application code.
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic