• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference in @PersistenceUnit and @PersistenseContext

 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

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

Thanks
 
Bod Toki
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • 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]
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • 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!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic