Saturday, February 10, 2007

Hibernate + Derby

Stumbled across Derby today. A few years ago I used Cloudscape (now owned by IBM) for a toy project. Now with the open-source code maintained by Apache, I decided to try it out.

Downloaded and unpacked the Derby jar files. Then dug up a Hibernate "hello world" project that is on my hard disk. This project was created by following the Hibernate reference document. It is a simple program with HSQL as the database.

I made a copy of the project. Edited the Hibernate configuration file as follow:

<property name="connection.driver_class">
org.hsqldb.jdbcDriver
</property>
<property name="connection.url">
jdbc:hsqldb:hsql://localhost
</property>
<property name="connection.username">
sa
</property>
<property name="connection.password">
</property>
<property name="connection.driver_class">
org.apache.derby.jdbc.EmbeddedDriver
</property>
<property name="connection.url">
jdbc:derby:dbname;create=true
</property>

......

<property name="dialect">
org.hibernate.dialect.HSQLDialect
</property>
<property name="dialect">
org.hibernate.dialect.DerbyDialect
</property>

......

<property name="hbm2ddl.auto">
create
</property>

......



The text colored in red is to instruct Derby to create the database and for Hibernate to create the DDL. Only required for the first run. They can be commented thereafter.


Then went on to modify the class path to include the Derby jar file. And the Hibernate version that I have already has the Dialet for Derby. So no need to search around for the class.

Fired the program up. And without modifying any Java code, the whole program was now "ported" to another database. All it took was just about 15 minutes.

No comments: