Skip to main content

Glassfish Persistence (Part #2)

Posted by tchangu on May 29, 2006 at 9:14 PM PDT

Switching the Java Persistence provider is straight forward and seamless if project /product uses Java Persistence API (EJB3.0) either as a standalone framework component or in a container managed environment. In my earlier blog entry, I had used TOPLINK as a provider and switching to use hibernate as a provider is fairly easy.

Here is what one need to do. None of the source code changes but the persistence.xml and the classpath need to change.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="" version="1.0">
   <persistence-unit name="test" >
         <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
         <property name="hibernate.connection.username" value="vk"/>
         <property name="hibernate.connection.password" value="password"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost/persistence"/>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
         <property name="" value="update"/>

The classpath needs to be updated to include the hibernate jar files. The downside is that there are many jar files that one needs to worry about. This was rightly pointed out in the excellent article by Joshua Marinacci that was published on I will wait for the next on the series by Joshua.

Out of curiosity I ran both of the implementation through the Netbeans profiler trying to push 100 rows of customer/order data to my local mysql database.

Here is the hibernate profile


and the toplink profile


The results are interesting. The time spent seems to be different for different provider. With Hibernate as provider good % of the time is spent creating the EntityManagerFactory using the factory method on the Persistance class. However while using Toplink as provider, the bulk of time is spent on creating the entity manager and commiting the transaction.

However, during the course of moving the code around while profiling, I noticed something weird. If in my client code I were to switch from

for (int i=0; i < 100; ++i){
   System.out.println("Inserting Customer and Orders... " + i + " " + testInsert(i));

for (int i=0; i < 100; ++i){
   System.out.println("Inserting Customer and Orders... " + i + " " + testInsert(i));

(where testInsert inserts customer and order mentioned in my previous blog entry) using toplink as persistence provider resulted in fewer rows being inserted to the table. However using hibernate resulted in expected number of rows. The following screenshots are after running the same code for hibernate and toplink provider. I am not 100% sure if the bug is in toplink implementation or in my customized sample code.





Related Topics >>