Websphere, Hibernate, JPA, with a twist of Classpath hell

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:

  1. Shutdown WebSphere
  2. Download the IBM Install Manager at http://www-01.ibm.com/support/docview.wss?uid=swg24027833 if you don’t already have it.
  3. Start the IBM Installation Manager
    1. Make the Install Manager aware of your WebSphere installation if it isn’t already
      1. Select IMPORT from the main page
      2. Select the installation directory of your copy of WebSphere
      3. Select next for the rest of the steps until finished
    2. Back at the main IBM Installation Manager screen select INSTALL
      1. Select the Feature Pack for OSGi Applications and JPA 2.0
      2. Install the Feature Pack
    3. Once done, exit the IBM Installation Manager
  4. Start the IBM Profile Manager
    1. In the Profile Manager, Select the Server then click AUGMENT
    2. Select the JPA 2.0 and click NEXT then click INSTALL
    3. Exit the IBM Profile Manager
  5. Start up WebSphere
    1. To Verify that the install took:
      1. Go to the web interface for the WebSphere server
      2. Select SERVERS -> SERVER TYPE -> WEBSPHERE APPLICATION SERVERS -> SERVER1 -> RUNTIME -> PRODUCT INFORMATION
      3. You should see ‘Feature Pack for OSGi Applications and Java Persistence API 2.0’
    2. Uninstall your .war, .ear, .etc and reinstall it

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

About sseaman

Connect with me on Google+
This entry was posted in Java, Programming. Bookmark the permalink.

19 Responses to Websphere, Hibernate, JPA, with a twist of Classpath hell

Leave a Reply

Your email address will not be published. Required fields are marked *