Using Hibernate as the Persistence Manager on GlassFish
By johnc on May 17, 2006
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">
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.