 |
Using OpenJPA as Java Persistence API provider in GlassFish
Posted by ss141213 on July 27, 2006 at 03:31 PM | Comments (6)
As this theserverside.com 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/openjpa-0.9.0.zip
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/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="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name ="em1">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
</persistence-unit>
</persistence>
Step #5: Build and deploy your app
Now you can build your Java EE application and deploy it to GlassFish.
Conclusion:
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.
Resources
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:
geronimo-j2ee-connector_1.5_spec-1.0.1.jar
geronimo-jms_1.1_spec-1.0.1.jar
geronimo-jta_1.0.1B_spec-1.0.1.jar
persistence-api-1.0.jar
derby-10.1.3.1.jar
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 glassfish persistence
Thanks.
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Great Sahoo! You have contributed alot to persistence in Glassfish!
Posted by: ss144236 on September 20, 2006 at 02:07 AM
-
Hi Sahoo,
Minor detail: While I was trying this, I found out that you also have to copy theopenjpa-[version[\]-incubating-SNAPSHOT.jar to the directory of glassfish
Posted by: blankema on October 17, 2006 at 07:40 AM
-
Yes, you are right. There seem to be a change in the way OpenJPA is packaged these days. openjpa-{version}-incubating-SNAPSHOT.jar is no more located in $OPENJPA_HOME/lib. It is installed in $OPENJPA_HOME. So, step #3 should be:
cp $OPENJPA_HOME/openjpa-{version}-incubating-SNAPSHOT.jar $OPENJPA_HOME/lib/*.jar $GLASSFISH_HOME/domains/domain1/lib/
Thanks for pointing this out. I will change the original blog later on.
Sahoo
Posted by: ss141213 on October 17, 2006 at 08:36 AM
-
Hi Sahoo,
I have a problem with exception like : org.apache.openjpa.persistence.ArgumentException: No table was given for persistent type...
I have copied all jar files and openjpa incubating file to $GLASSFISH_HOME/domains/domain1/lib/ .
Do You know how can I fix this problem ?
I use the Glassfish v2 and try to use OpenJPA ( v0.9.6 and 0.9.7)
regards,
Posted by: armad on April 11, 2007 at 11:50 PM
-
Hi armad,
The only time I had come across "No table was given for persistent type.." type of error while using OpenJPA 0.9.0 was caused by the fact that I had *not* copied *openjpa-persistence-jdbc-0.9.0.jar* into domain1/lib folder. The error message was useless; an OpenJPA developer explained to me that because of the highly pluggable nature of OpenJPA, sometime the error messages can be misleading.
Since you already mentioned that you have already copied all the OpenJPA jars to domain1/lib, I don't know what else could be going wrong. Can you double check?
Have you tried asking this question in OpenJPA forum?
Thanks,
Sahoo
Posted by: ss141213 on April 12, 2007 at 12:46 AM
-
Hi Armad,
Can you try a simple test outside GlassFish? I tried persistence example from GlassFish persistence page with OpenJPA quite recently and it worked as expected.
thanks,
-marina
Posted by: mvatkina on April 12, 2007 at 09:46 AM
|