(Wrote this way back in 2008, and copied over to this blog recently)
Java: Using Apache Derby with Hibernate
Today, I came across a need to have an extremely light database. The database is to be used to store report formats. Formats change very infrequently. Its an internal application, used by only 1 user. In the coming years, the user count may grow to 10. So, the load on the database would be negligible.
Big databases like Oracle and Sybase would be an overkill for this, so I started looking at small / light weight embeddable database. Being a Java Programmer, I stuck with Java database. The choice boiled down to 2 databases HSQLDB and Apache Derby. From the documentation, it seemed like some additional configuration would be required to make HSQLDB save contents to a file. Being a lazy developer, I started looking into Derby. Apache Derby has an interesting history. When I found out that Java 6 includes Derby, it kind of sealed the deal for me. So… Derby it is.
Started going through the documentation, I quickly wrote a small script that would allow me to access my database. A little elaborate, but gets the job done:
db.sh
So far, so good. I created a database at the command prompt and tries it out. Pretty nifty.
Now, next challenge: How do I integrate derby with Java and Hibernate? I wasn’t sure even if hibernate supported Derby. Given, Java’s support, I had a feeling that it did. Google revealed that my feeling was right. Found information scattered all over on how one could use hibernate with derby, but no central HOWTO. So, here it is:
Create a hibernate.cfg.xml file. I chose to name mine hibernate-derby.cfg.xml:
After this, its pretty much straightforward Java / hibernate code:
Just for completeness sake, here are the rest of the files: