Deploying application using Hibernate JPA 2.1 to IBM WebSphere gives NullPointerException

I share what worked for me. Me and my team were able to deploy our application with Spring 4.2.4, Hibernate 5.1 and Hibernate JPA 2.1 in Websphere 8.5.5 and Websphere 7.

The key to do this is to extract all the hibernate libraries and put them in a shared library folder in your server, create in your server configuration a new class loader with parent last order and assign to this class loader the shared library containing the hibernate libraries. Then you must change the Default Java Persistence API settings to use the hibernate persistence provider and not the default WebSphere persistence provider. So the steps are the followings:

  1. Create a folder in your server and put in it all the hibernate libraries

    enter image description here

  2. Create the shared library in Websphere (Environment / Shared libraries) and set it to the path of the folder you just created on the server

  3. On WebSphere click on your server and under Server Infrastructure click on Java and Process Management / Class Loader. Click on New and select the Parent Last option. Save

    enter image description here

  4. Click on the new class loader you just created and click on Shared library references on the right. Here add the shared library containing the Hibernate Libraries

  5. Now under the section Container Settings of your server click on Container Services / Default Java Persistence API settings. Here you have to specify an alternate default persistence provider. See the image below.

    enter image description here

  6. Now compile your application and remove from the generated war the Hibernate libraries (see image 1). Then install it in the server.

In my case the application works like a charm also in WebSphere 7. This procedure solves javax/persistence/Table.indexes() error and all the other compilation error caused by libraries conflict between WebSphere and Hibernate.

Reference:

https://www.ibm.com/developerworks/community/wikis/form/anonymous/api/wiki/53181ccd-bcd4-431f-b968-0b5f6d46d652/page/192a432b-28bb-4080-b037-345e5d83da76/attachment/61e74f67-1d60-4120-ba25-ad7264c9f4f6/media/AlternateJPAProviders_TestReport.pdf


The javax/persistence/Table.indexes() is from JPA 2.1, WebSphere Application Server traditional 8.5.5.2 is using by default JPA 2.0 (OpenJPA). If you want to use Hibernate you have to override the default provider - check this post for more details.

UPDATE

Yes, you are correct. You cannot override default provider in WebSphere traditional 8.5.5.x with JPA 2.1 as container relies on 2.0 api for initialization.

If you have to use 2.1 you may use it as application managed provider not container managed. So you would need to disable JPA for your application, check this and instead of rely on container on injecting EntityManager, initialize it by your self using API.

The other option would be to consider WebSphere Liberty which supports JPA 2.1 (using EclipseLink) starting from version 8.5.5.6. It should be possible to change provider there since container supports 2.1, however I didn't have time to test it.

You can also upgrade to WebSphere Application Server v9, recently released, which supports JPA 2.1.


Thought I would share what worked for me. I was able to use JPA 2.1 and Hibernate 4.3.11 on Websphere 8.5.5.5 by using a custom PersistenceProviderResolver so that the Websphere classes do not interfere with Hibernate https://gist.github.com/jeffsheets/aec3e94870ef903ce7efe33e00563d3c

I based it off of a post found here https://hibernate.atlassian.net/browse/JPA-4