Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Difference in @PersistenceUnit and @PersistenseContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Difference in @PersistenceUnit and @PersistenseContext" Watch "Difference in @PersistenceUnit and @PersistenseContext" New topic
Author

Difference in @PersistenceUnit and @PersistenseContext

Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
Hello

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

Thanks


"Know where to find the solution and how to use it - that's the secret of success."
Bod Toki
Ranch Hand

Joined: Sep 01, 2006
Posts: 95
@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

Joined: Oct 16, 2005
Posts: 1277
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!

SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Yes, I missed the Persistence.createEntityManager() method! Thanks!
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
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

Joined: Sep 01, 2006
Posts: 95
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference in @PersistenceUnit and @PersistenseContext