Use Hibernate 4.3.x with JTA on Glassfish 4
This will allow you to upgrade to JPA 2.1 using the new Hibernate framework on Java EE 7 and maintain your JTA datasources with a standard JPA EntityManager.
First you will want to download Hibernate 4.3.x from http://hibernate.org/orm/downloads/ and drop all the required jars in the zip in {Glassfish4 direcory}/glassfish/lib
I also installed jboss-logging-3.1.x.GA.jar
in the {Glassfish4 direcory}/glassfish/lib/endorsed
folder to allow Hibernate logging.
I recommend using Maven for your project dependency management since Hibernate has a large dependency tree. Add the following dependency to pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.0.Final</version>
</dependency>
In Hibenate 3.6 and JPA 2.0 your persistence.xml
file would look like this:
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="myunitname" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
...
</persistence-unit>
</persistence>
Now with Hibernate 4.3 You must update your persitence version and your provider class:
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="myunitname" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/my-data-source</jta-data-source>
<properties>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
...
</properties>
</persistence-unit>
</persistence>
Note the addition of the property hibernate.transaction.jta.platform
This is required and the value will change depending on your container. For Glassfish it's org.hibernate.service.jta.platform.internal.SunOneJtaPlatform
. See here for other platform implementations: http://docs.jboss.org/hibernate/core/4.0/javadocs/org/hibernate/service/jta/platform/internal/package-summary.html
Written by Brian Jesse
Related protips
4 Responses
Thank you so much :)
Thanks man, saved me a lot of headache!
Good Afternoon,
I was following your guide with the change that I'm using JPA 2.1 with Hibernate 4.3.6.Final in Glassfish 4.
The thing is that I'm using netbeans to migrate a Maven Project from Glassfish 3 to 4 and after copying and pasting all the .jars from Hibernate when I Clean and Build the project I'm still getting a java.lang.ClassCastException which is related to an database entity that is trying to be casted as the same class in a for loop.
Another error that I'm getting is with the java.lang.EJBException which I don't know why it is generated.
Could you please advice me what steps or dependencies I need to define on the Maven's pom.xml file in order to have this working correctly?
Thank you.
Why does jboss-logging-3.1.x.GA.jar need to be placed in the endorsed directory?