Skip to main content

Using OpenJPA as Java Persistence API provider in GlassFish

Posted by ss141213 on July 27, 2006 at 3:31 PM PDT

As this announcement shows, although OpenJPA is still in the Apache incubator and not yet certified as a Java Persistence API compliant provider, it is not far from that stage. There is a source download available and I must say, it is quite easy to build. If you want to use it in GlassFish, then it's even easier. It is as simple as these intuitive steps:

Step #1: Download and install GlassFish
Download any of the latest builds of GlassFish v2 or v1_ur1 from here. Install it in any directory of your choice. I refer to that as GLASSFISH_HOME.

Step #2: Build & Install OpenJPA
Build OpenJPA using these instructions. It's really simple to build. The build process produces a zip file called openjpa-project/target/filtered-site/resources/downloads/
Unzip this to any location. In this blog, I refer to that location as OPENJPA_HOME.

Step #3: Install OpenJPA in GlassFish
All you need to do is to make OpenJPA implementation jar files available to GlassFish runtime. This can be achieved as follows:

 cp ${OPENJPA_HOME}/openjpa*.jar ${OPENJPA_HOME}/lib/*.jar ${GLASSFISH_HOME}/domains/domain1/lib 

GlassFish will automatically make this jar part of its classpath next time when you restart the server.

Step #4: Edit persistence.xml
Make a one line change to your persistence.xml to instruct GlassFish that we want to use OpenJPA as the persistence provider for this application. This is achieved by setting
element to org.apache.openjpa.persistence.PersistenceProviderImpl. Shown below is a sample persistence.xml:

<persistence xmlns="" version="1.0">
    <persistence-unit name ="em1">

Step #5: Build and deploy your app
Now you can build your Java EE application and deploy it to GlassFish.

As you can see, it is fairly simple to use any third-party provider in GlassFish. We had earlier talked about using Kodo and Hibernate in GlassFish. Please let us know, if you want any particular provider to be used in GlassFish that's not covered yet.

If you want some working applications, then you can download them from my earlier blogs:
a) a web application that uses Java Persistence API -- download the complete sample from here
b) a multi-tier Java EE application (web->ejb->JPA) -- download the complete sample from here.
After downloading, you need to change persistence.xml as described above. Then you can use the existing build.xml to build the app.

You don't have to read this if you followed the above instructions:
I will share a problem that I ran into. I did some static analysis of the OpenJPA provider class using a little static analyser tool and figured out a list of dependent jar files. First time when I tried OpenJPA in GlassFish, I only copied the list of jars that I inferred from the static analysis phase. But I never realized that OpenJPA loads a lots of classes using reflection. I never imagined that not having a dependent jar file in CLASSPATH could lead to an exception like
org.apache.openjpa.persistence.ArgumentException: No table was given for persistent type example.UserCredential even when table for that entity existed in database. Thanks to Pinaki who pointed out that OpenJPA uses Jar Service Provider mechanism quite a lot so that its Kernel can be configured for either JPA or JDO environment. As a result of this, unless all the services are available in CLASSPATH, the kerner can't be correctly configured and that unfortnately leads to very weired exceptions. When I copied all the jar files from $OPENJPA_HOME/lib to $GLASSFISH_HOME/domains/domain1/lib, it worked.

Having said that, you actually don't need to copy the following jar files as GlassFish runtime already have those classes:
It's no harm done even if you copy these jar files because GlassFish will simply ignore them.

Hope you find this blog useful. More blogs about


Related Topics >>