Switching of JPA provider in a JEE container is a common question. This article gives some hints on the way to use Hibernate with TomEE.
There are several ways to do it. The main ones are either to provide Hibernate in the container or to provide it in the application (let say a webapp for this post).
Update the container to provide hibernate
This solution is pretty easy but needs to update the container (not always appreciated by integration/production teams).
You can follow the following documentation: http://openejb.apache.org/tomee-and-hibernate.html
But personally i prefer to use maven to do the work. So simply checkout openejb/tomee trunk:
svn co http://svn.apache.org/repos/asf/openejb/trunk/openejb/
Then simply compile it with the hibernate profile (skip tests since some of them depends on OpenJPA):
mvn clean install -Dmaven.test.skip=true -pl tomee/apache-tomee -am -P hibernate
Then simply take the archive you need (webprofile, jaxrs or plus version of tomee) in tomee/apache-tomee/target folder and deploy your application as usually.
Provide Hibernate in the webapp
Another way to do it is to provide Hibernate in your webapp.
You’ll typically add this dependency to your pom if you use maven (or adapt it for ivy, gradle…):
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> <!-- 4.1.4.Final for instance --> <exclusions> <exclusion> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> </exclusion> <exclusion> <groupId>org.jboss.spec.javax.transaction</groupId> <artifactId>jboss-transaction-api_1.1_spec</artifactId> </exclusion> </exclusions> </dependency>
Package your webapp and deploy it specifying org.hibernate.ejb.HibernatePersistence as provider in your persistence.xml.
For tomee trunk it is enough but for older versions you’ll need to provide a class to help hibernate to get the TransactionManager of TomEE. You can find such a class for hibernate 4 here: https://hibernate.onjira.com/browse/HHH-7401
Another way to get this class (and this tip works for eclipselink, and hibernate 3 too) is to add openejb-jpa-integration jar (available on trunk in snapshot version) as a dependency in your webapp.
Note: excluding javassist from hibernate dependencies can be a great idea too since openwebbeans uses it and javassist is provided in common lib
Hello,
Thanks for this tuto.
Do you need to have this maven dependency ?
org.hibernate.javax.persistence
hibernate-jpa-2.0-api
1.0.1.Final
Actually JavaEE API (including jpa one) are packaged in tomee/openejb so no need of them. That’s why it was excluded.