ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

What Is Hibernate
Pages: 1, 2

When to Use Hibernate

Given how cool and flexible Hibernate seems, why would you ever use anything else? Here are a some scenarios to help you make that judgment (and perhaps, by providing some contrast and context, they'll help identify situations where Hibernate is the perfect fit).

If your application has very simple data storage needs--for example, you just want to manage a group of user preference choices--you don't need a database at all, let alone a fancy object-relational mapping system (even one as wonderfully easy to use as Hibernate)! Starting with Java 1.4, there is a standard Java Preferences API that fills this role very nicely. (More on the Preferences API can be found in this ONJava article.)

For people who are already comfortable working with relational databases and know how to craft the perfect SQL query to interact with an enterprise database, Hibernate might seem to get in the way, much like a land yacht with power everything and automatic transmission can irritate a performance-oriented race car driver. If this describes you, if you're on a project team that includes a strong DBA, or are given stored procedures to work with, you might want to investigate iBATIS. The authors of Hibernate themselves identify iBATIS as an interesting alternative. I found it intriguing because we developed a similar (though much less powerful) system in-house for an e-commerce site and we've been using it in other contexts ever since, although after discovering Hibernate we usually favor that for new projects. You might consider SQL-centric solutions like iBATIS to be "inverted" object/relational mapping tools, whereas Hibernate is a more traditional ORM.

Of course, there may be other external factors that mandate a different approach. You might have to use a full-blown EJB architecture (or some other non-plain-objects mapping system) to fit in with an enterprise environment. You may be tailoring code for a platform that provides its own data storage tools, like Mac OS X's Core Data. You may have been handed a storage specification like an XML DTD, which doesn't involve relational databases at all.

But if you're working with a rich object model, and want to be able to store it flexibly, easily, and efficiently (whether or not you're starting with or have decided on using a relational database, as long as that's an option--and with good free databases like MySQL or the Java embeddable HSQLDB available, it ought to always be an option), then Hibernate is probably the way to go. You may be amazed at how much time you save, and how much you love working with it.

Learning More

The Hibernate project has extensive online documentation that can help orient you and get you started quickly.

The definitive reference is Hibernate in Action, by Christian Bauer and Gavin King. Written by the creators of Hibernate, it gives you a thorough grounding in what you can do with the package, and how to do it right.

My own book, Hibernate: A Developer's Notebook, is another great way to get started quickly. It provides a direct but detailed walkthrough of how to set up Hibernate in a Java project, and how to use some of its most important features. The code examples are currently based on earlier releases of Hibernate and HSQLDB, so if you want them to work without any tweaks (which is the nicest way to get started), you'll need to work with those versions of the software. The basic concepts remain on target, regardless, and I'm hoping to be able to update the book for Hibernate 3 soon.

Another interesting book is Better Faster Lighter Java, by Bruce Tate and Justin Gehtland. This book's pragmatic approach to getting real projects finished in a sane way is one of the reasons Hibernate spiked in popularity. It gives sound advice on how to evaluate and use (or reject) available Java technologies, and mentions both Hibernate and Spring as examples of the right approach.

Finally, "Working with Hibernate in Eclipse" (though it predates the new alpha Hibernate 3 tools, which are shaping up to be far, far cooler) walks through how to use an Eclipse plugin called Hibernate Synchronizer with Hibernate.

James Elliott is a senior software engineer at Singlewire Software, with fifteen years' professional experience as a systems developer.

Return to ONJava.com.