Problem
Recently I had to take a .war project that utilized Hibernate 3.5 and JPA 2.0 and deploy it in WebSphere 7.0. It has been quite a long time since I worked with WebSphere and was very happy to find that it was very easy to setup and deploy my existing war.
What I didn’t expect was the Classpath mess that was to ensue and the 7 hours of research to find a solution to what appears to be a fairly common problem (but no real solution defined)
You see, WebSphere ships with a copy of OpenJPA. OpenJPA is a JPA implementation that WebSphere provides for use by developers. It just so happens though that the OpenJPA that WebSphere ships with is a JPA 1.0 implementation.
Hibernate 3.5 or > requires a JPA 2.0 implementation. Houston, we have a problem.
No problem you think. I’ve got the JPA 2.0 implementation (hibernate-jpa-2.0-api01.0.0.Final.jar) in my .war’s classpath, so I’m fine. Right?
Wrong. WebSphere puts OpenJPA on the Classpath first, no matter what. You can change the configurations (Parent first/Parent last) and it will still have OpenJPA on the Classpath before the .war file. You’ll start seeing a lot of weird errors like:
java.lang.NoSuchMethodError: javax/persistence/spi/PersistenceUnitInfo.xxxx
If you see the above you have a JPA 1.0/JPA 2.0 issue because of OpenJPA.
Solution
IBM has a Feature Pack (think library upgrade package) for WebSphere to get it to run OSGi and JPA 2.0. Once you install this everything should work fine.
Steps:
- Shutdown WebSphere
- Download the IBM Install Manager at http://www-01.ibm.com/support/docview.wss?uid=swg24027833 if you don’t already have it.
- Start the IBM Installation Manager
- Make the Install Manager aware of your WebSphere installation if it isn’t already
- Select IMPORT from the main page
- Select the installation directory of your copy of WebSphere
- Select next for the rest of the steps until finished
- Back at the main IBM Installation Manager screen select INSTALL
- Select the Feature Pack for OSGi Applications and JPA 2.0
- Install the Feature Pack
- Once done, exit the IBM Installation Manager
- Make the Install Manager aware of your WebSphere installation if it isn’t already
- Start the IBM Profile Manager
- In the Profile Manager, Select the Server then click AUGMENT
- Select the JPA 2.0 and click NEXT then click INSTALL
- Exit the IBM Profile Manager
- Start up WebSphere
- To Verify that the install took:
- Go to the web interface for the WebSphere server
- Select SERVERS -> SERVER TYPE -> WEBSPHERE APPLICATION SERVERS -> SERVER1 -> RUNTIME -> PRODUCT INFORMATION
- You should see ‘Feature Pack for OSGi Applications and Java Persistence API 2.0’
- Uninstall your .war, .ear, .etc and reinstall it
- To Verify that the install took:
The errors should disappear and everything should start working!
Things to note
- You need to be running WebSphere 7.0.0.9 or greater. If you aren’t, you can’t use the Feature Pack
- You may need to define the persistence provider in your persistence.xml file. I didn’t have to but I found many sites suggesting that it was the solution (it wasn’t, but it couldn’t hurt). If you want to try it:
- Add
<provider>org.hibernate.ejb.HibernatePersistence</provide>
between your<persistence-unit>
tags - You will notice Hibernate starts up as soon as your app starts in WebSphere. If you haven’t fixed the above error (
java.lang.NoSuchMethodError
) you will just see it sooner and your app won’t start
- Add
19 Responses to Websphere, Hibernate, JPA, with a twist of Classpath hell