03 October 2011

Configuring Hibernate as JPA implementation provider in GlassFish

This post helps to configure Hibernate as JPA implementation provider replacing the bundled EclipseLink.

I am using Maven and mine is an EAR containing a WAR and an EJB module. Hence in the pom.xml of the EAR module, the following dependency is mentioned.

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.7.Final</version>
         </dependency>

 
As this jar has to go into the lib folder directly under EAR ROOT, specify the value for defaultLibBundleDir (in pom.xml)

               <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <version>6</version>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>


Finally your persistence unit in persistence.xml

 <persistence-unit name="yourPU" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>jdbc/yourDataSourceName</jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup" />
        </properties>
    </persistence-unit>