geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Jencks <>
Subject JPA instructions
Date Thu, 09 Nov 2006 08:20:16 GMT
The latest weekly build of geronimo-jetty-j2ee includes support for  
jpa.  Here are some brief instructions.  Note that this does not  
include all the nifty jee 5 features such as resource injection and  
deployment locating your persitence.xml file for you.  However both  
container managed and application managed persistence is supported:  
there is support for extended persistence contexts but this has not  
been tested at all yet.

To deploy a persistence unit, you need a persistence.xml file that  
includes all the usual stuff and more specifically the datasources  
you wish to use.  To specify the datasource include an  
AbstractNameQuery sufficient to locate the  
ManagedConnectionFactoryWrapper gbean supplying the datasource.  Do  
not include any interfaces.

For instance, if you  deployed your datasource with name jdbc/ 
TradeDataSource and made sure this was in the ancestors of your app  
you would specify

Note that if you use openjpa and want a sequence to assign generated  
ids you need to also include a non-jta-data-source.

Include the persistence.xml contents directly in your geronimo plan  
at the end where gbean definitions go.  For instance, for an ejb jar,  
you'd put it at the end of openejb-jar.xml like:

             <persistence xmlns=" 
persistence" version="1.0">

                 <persistence-unit transaction-type="JTA"  

                         <property name="openjpa.jdbc.DBDictionary"  
name="openjpa.jdbc.SynchronizeMappings" value="buildSchema 
                         <property name="openjpa.Sequence"  
value="table(Table=OPENJPASEQ, Increment=100)"/>


In the environment/dependencies element you need to include your jpa  
provider as a dependency.  We've packaged up the openjpa jar and its  
dependencies into a configuration so you can just add:


and you won't have to track down the right serp version etc.

So now if you deploy your app the persistence unit will get deployed  
and started for you.... but you might want to use it in your  

We aren't processing jee 5 deployment descriptors or the equivalent  
annotations so you have to use geronimo specific xml in your plan.   
Here's an example for a container managed persistence context  
accessed from a session bean:


  from an openejb-jar.xml.

Now your session bean can get the transaction-scoped container  
managed EntityManager like this:

     public void setSessionContext(SessionContext sc) {
         context = sc;
         if (sc != null) {
         try {
             entityManager = (EntityManager) new InitialContext 
         } catch (NamingException e) {
             throw new EJBException("could not get Naming Context", e);
         } else {
             entityManager = null;


If for some reason you want to use application managed persistence  
contexts you'd include something like this in your plan:


Looking up the EntityManagerFactory looks like this:

             EntityManagerFactory entityManagerFactory =  
(EntityManagerFactory) new InitialContext().lookup("java:comp/env/jpa/ 

Given some public outcry we could make the xml tag names more similar  
by changing persistence-context to entity-manager in the first style  
of xml.



I recommend you pre-enhance your classes as you build your app.  If  
you use maven 2 there are some examples of how to do it in the  
samples mentioned below.

If you wish to live dangerously and use runtime enhancement, that  
works too, sometimes.  The current limitation is that no gbean datas  
can include classes or objects of classes that need to be enhanced,  
since the gbean datas are deserialized before the bytecode  
transformer is installed.  One way you can get into trouble with this  
is if a web service endpoint uses jpa persistence-capable data beans  
as method arguments.

In order to get runtime enhancement you need to start the server with  
our jpa agent with a command line like this:

java -javaagent:bin/jpa.jar -jar bin/server.jar

This works with the simple test app but not with daytrader due to the  
web service problem just mentioned.


So far there are the following examples:

-- a very simple test app that requires you to  build the sandbox/ 
javaee5 stuff and run the geronimo-jetty6-jee5 server.  It's located  
in sandbox/javaee5/itests/jpa.

-- daytrader trunk.  This is converted so jpa is an option.  Deploy  
with the plan daytrader-jpa-plan.xml in daytrader/trunk/plans.  After  
you deploy daytrader you need to first visit the configuration page  
and set the persistence method to JPA.  Then populate the database  
and start trading.  I don't know if the old direct and ejb  
persistence methods still work: in particular I'm pretty sure the id  
generation methods are incompatible so if you want to try changing  
persistence method, clear the data and repopulate.

Try it out and please report problems

david jencks

View raw message