Use Hibernate as a persistence provider with Glassfish

As you must be aware, Glassfish currently has Toplink Essentials as the default persistence provider. The question that frequently gets asked is: How hard it is to to use hibernate as the persistence provider with Glassfish?
The answer is:  Not at all :). Just drop couple of jars reqired by hibernate in the lib directory of Glassfish and modify your persistence.xml to use hibernate as the provider and you are ready to go. Lets walk through the steps:

1. Download and install glassfish milestone 6 build or later.
2. Download and install Hibernate3 3.2cr1 or later.
3. Download and install Hibernate EntityManager 3.1 beta7 or later.
4. Copy the required hibernate jars into Glassfish lib dir. The script here contains the list of jars.
5. Update your persistence.xml to use hibernate as the persistence provider. Here is a sample persistence.xml
6. Deploy and run your application.

 

Comments:

If you're using Spring 2.0 what is the databasePlatform? It was oracle.toplink.essentials.platform.database.MySQL4Platform What is the hibernate equivalent? <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="generateDdl" value="true" /> <property name="databasePlatform" value="" /> </bean>

Posted by Gene De Lisa on June 02, 2006 at 06:44 AM PDT #

Please note that Hibernate is not able to deal with spaces in the full path to deployed application. If you plan to use Hibernate as the persistence provider inside Glassfish, please make sure that the Glassfish installation path does not contain spaces. Please see Glassfish discussion forum threadfor more discussion on this

Posted by Mitesh on June 28, 2006 at 04:23 PM PDT #

The script that Mitesh provides copies hibernate-annotations.jar from hibernate-entitymanager/lib dir. This may not always work. One needs to use compatible version of Hibernate Annotations(http://hibernate.org/247.html). See http://forums.java.net/jive/thread.jspa?threadID=16875&tstart=0 -- Sahoo

Posted by Sahoo on July 17, 2006 at 03:21 AM PDT #

Also note that this doesn't seem to work with Hibernate core 3.2.0-cr4, you have to downgrade to hibernate 3.2.0-cr2. It seems to work with annotations and entity manager versions 3.2.0-cr1

Posted by Joe on August 27, 2006 at 03:37 AM PDT #

Hello! I just tried that with Hibernate 3.2.1-GA and hibernate entity manager 3.2.1-GA. I'm using PostgreSQL and sequences in it for primary key generation. Now I'm getting an error "org.hibernate.AnnotationException: Unknown Id.generator" on it. Do you know what it may be or how to fix it ?

Posted by Mr.K on December 09, 2006 at 04:31 AM PST #

No go. I am getting an exception with the method described:

com.sun.enterprise.deployment.backend.IASDeploymentException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [mypackage.MyClass/myPU] in the scope of the module called [].

To this day I haven't been successful getting any persistence framework to run with Glassfish with the exception of the provided Toplink, which is extremely slow.

Posted by Ulrich Mayring on June 13, 2007 at 07:12 PM PDT #

Got rid of the above error, got the newest, compatible annotation jars, now I have this:

java.lang.NoClassDefFoundError: javax/transaction/SystemException

Ulrich

Posted by guest on June 13, 2007 at 07:50 PM PDT #

Hi all,

Thanks a lot, your description helped me a lot. I just did it. It took me something like 30 minutes (something that can help is watching out the compat' matrix to put compatible HEM, H3 and HA jars together: http://www.hibernate.org/6.html#A3)

Is there a way to change the default provider?
I mean: be able to use Hibernate as the default persistence provider without specifying <provider>org.hibernate.ejb.HibernatePersistence</provider>?

I'm going to have a look at it, so if someone already did it, I'd be delighted to have some insights :).

Cheers.

Posted by Batmat on June 24, 2008 at 02:32 AM PDT #

rffsf

Posted by Eldar on August 27, 2008 at 08:47 PM PDT #

Hi,
We are using JPA with Hibernate and Glassfish 9.0.
Hibernate log all debug and Info.We dont want all the things. How to configure the logging level of hibernate properties in glassfish.

Posted by Sankar on September 02, 2008 at 12:11 PM PDT #

Hi Sanakar,

Hibernate uses log4j as its logging framework. Please refer to Hibernate documentation to configure logging level.

Thanks,

Posted by Mitesh Meswani on September 03, 2008 at 09:35 AM PDT #

This is the list of jars to be placed into glassfish/lib directory for latest Hibernate release (hibernate-entitymanager-3.4.0.GA and hibernate-distribution-3.3.1.GA):

antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate3.jar
javassist.jar
jta-1.1.jar
log4j.jar
slf4j-api-1.5.2.jar
slf4j-log4j12.jar

Note that the last jar is located under hibernate-entitymanager-3.4.0.GA/lib/test

Posted by Marina Vatkina on October 28, 2008 at 08:07 AM PDT #

Thanks for taking the time to talk about this, I feel fervently about this and I take pleasure in learning about this topic. Please, as you gain information, please update this blog with more information. I have found it very useful

Posted by prada sunglasses for men on December 13, 2010 at 02:48 PM PST #

I think I found an easier way. Instead of putting the Hibernate libs in the Glassfish server lib directory you can build an EAR and include them within the EAR's lib directory. I just posted a blog entry on our experiences with this approach. IMO it's better to bundle Hibernate with the application itself instead of changing the server's global configuration. By doing so it wont affect other applications that may also be running on the same server (e.g., legacy applications). See http://javafromthetrenches.wordpress.com/2011/01/15/issue-how-to-use-hibernate-jpa-with-glassfish-instead-of-toplink/ for more info.

Steve

Posted by Steve Nies on January 15, 2011 at 03:53 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Mitesh Meswani

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks