persistence@glassfish.java.net

re: Use same persistence unit in ejb and web tier

From: Ian Evans <Ian.Evans_at_Sun.COM>
Date: Mon, 23 Oct 2006 15:26:55 -0700

Benjamin Graf <Benjamin.Graf_at_gmx.net> wrote:
> I'm looking for a possibility to share a persistence unit in the web and the ejb
> part of a enterprise application but failed. I tried two ways:
>
> 1. Implement persistence unit in both parts with the same name, but I seems to
> address to individual persistence contexts in glassfish since data isn't
> synchronized.
>
> 2. Implement persistence unit only in the ejb tier throws NPE in web tier while
> using EntityManager.
>
> 3. The last but not yet tested solution may be to take all entity stuff into the
> ejb tier and make the web application use them to get the data from the database.
>
> Are there any better solutions?

I'll assume that your application is an EAR containing both a WAR and an
EJB JAR. In this case, you should package your entities into a JAR that
is then located either at the root of the EAR, or in the EAR's lib
directory:
MyEnterpriseApp.ear
-MyWebApp.war
-MyEJBs.jar
-MyEntities.jar

Or:
MyEnterpriseApp.ear
-MyWebApp.war
-MyEJBs.jar
-lib/MyEntities.jar

The persistence.xml file should be located in MyEntities.jar's META-INF
directory, and it should define one persistence unit. The scope of the
persistence unit is the EAR.

See section 6.2 of the Persistence spec and/or the Java EE 5 Tutorial:
http://java.sun.com/javaee/5/docs/tutorial/doc/PersistenceIntro3.html#wp81548

-ian
-- 
Ian Evans
ian dot evans at sun dot com
Java Enterprise Edition technical documentation