Monday 8 March 2010

PersistenceUnit - PersistenceContext

But what does it all mean???

I don't know if it is because at this point in time I am suffering from a cold and a pair of ulcers are having a party in my mouth. But I am struggling to come to terms with the difference between and PU and PC...

After a quick Google on @PersistenceUnit vc @PersistenceContext I have deduced the following:
A PU will give you an EntityManagerFactory
A PC will give you an EntityManager

Now when to use either, really comes down to your TX strategy, who is managing your connections etcetc.
Using Spring I have always used PC to inject in a Spring managed EM to do the do with. However why is this?
  • @PersistenceUnit annotation is used in J5SE applications to handle Java Persistence API (JPA) persistence management. You may use a PeristenceUnit in an application inside a container, but it must be managed by the developer instead of the container.
  • @PersistenceContext annotation is used for Container Managed Persistence (CMP). This relieves the developer of having to worry about connection management.
and I think this link describes this nicely:

http://java.dzone.com/tips/how-use-glassfish-managed-jpa