Using Hibernate as the Persistence Manager on GlassFish

There's been a lot of talk about using Java Persistence API to plug in Hibernate as the persistence manager instead of Oracle's toplink, so let's take a look at how you do this. First, you download the Hibernate Core 3.2.0 cr2 and Hibernate EntityManager 3.2.0 cr1 from the Hibernate website.

Then you copy over the correct JAR files from hibernate to the GlassFish lib directory. I can't figure out exactly which JAR files to copy so I copied hibernate3.jar, hibernate-entitymanager.jar, then all of the JAR files in each of hibernate-3.2/lib and hibernate-entitymanager/lib. I tried to read the READMEs in the lib folders to figure out which ones were really required but I was obviously leaving something out because my App Server wasn't starting correctly. If anyone out there knows which JAR files are strictly required please let me know.

Of couse, restart the server if it's already started.

Now start up the IDE and start the Java DB database. Create a web application project (in this case HibernateApp) and add the JSF framework to it in the second page of the New Project wizard. Right-click the project node and choose New > Entity Classes from Database. Set the jdbc/sample database as the Data Source, select the CUSTOMER table (this is an example table shipped with the IDE) and click Next.

Now it's time to create the persistence unit by clicking the Create Persistence Unit button. There's still some bugs that need to be worked out. In the final version the IDE will scan your GlassFish dir and list Hibernate in the Persistence Provider drop-down. For now, you have to create the persistence unit with TopLink and then manually change it in the persistence unit XML. Let's do that now. Set hibernate as the persistence unit name, leave TopLink as the persistence provider, and switch the table generation strategy to None. You have to do the final one because the syntax for the XML generated by TopLink and Hibernate are different.

When you click Finish, our persistence unit is created (persistence.xml under Configuration Files node) and the entity classes are created (under Source Packages). Double-click persistence.xml and swith to the XML view (click the XML button at the top of the editor). Change the provider element to org.hibernate.ejb.HibernatePersistence, as shown below:

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="hibernate" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/sample</jta-data-source>
<properties/>
</persistence-unit>
</persistence>

Now we can quickly generate JSF pages for the entity classes with Geertjan's favorite feature. Right-click the project node and choose New > JSF Pages from Entity Class. Add both Customer.java and DiscountCode.java to the list of selected classes and click Next. Change the package to hibernatesample.controller and click Finish (we won't specifiy a special folder for JSF pages).

And just Run the project. Hopefully it'll work. It didn't for me the first time, I got a 500 error. I restarted the server again and tried again and everything seemed to work. I guess GlassFish didn't register all of the Hibernate libraries first.

OK, now time to show my ignorance. Why would you want to do this? I mean, is it just a matter of personal preference? Is there a performance difference between TopLink and Hibernate? Any illumination you can shed on this would be helpful.

Comments:

1. Find all you need to do to run Hibernate in GlassFish here:

http://blogs.sun.com/roller/page/theaquarium?entry=running_hibernate_in_glassfish

There is a script that Mitesh provides that copies all the necessary jar files.

2. The development process should not be any different whether you use TopLink or Hibernate. So, I suggest, you just hand edit the persistence.xml and move on. Here is how my persistence.xml looks like when I use Hibernate:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="pu1">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        </properties>
    </persistence-unit>
</persistence>
Hope this helps. If you still run into problems and think issues have to do with GlassFish, ask questions in GlassFish forum. Sahoo

Posted by Sahoo on May 19, 2006 at 12:25 AM CEST #

Post a Comment:
Comments are closed for this entry.
About

johnc

Search

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