Hibernate in Action
by Christian Bauer and Gavin King
August 21, 2006
Christian Bauer and Gavin King are members of the core Hibernate team - Gavin is the founder and principal developer, Christian maintains Hibernate's documentation and website. This makes the book they've written a great starting point for in-depth exploration of Hibernate's features. With it you get the perspective of those who conceived it and created it.
My own first experience with ORM tools goes back to 1999 when I was asked to use TopLink (another leading ORM tool) on a project with an existing database schema requiring lots of grouping and aggregation - not an ideal use case for ORM technology as we learn from this book. Naturally the difficulties experienced on that project led me to read Hibernate in Action with a different point of view.
The book opens with a chapter summarizing what's known as the Object/Relational paradigm mismatch - a conflict between the worlds of object-oriented and relational modeling. This is arguably the most important part of the book. It lays out the motivation for creating Hibernate and may be one of the reasons for Hibernate's success at a time when other competing technologies did not fare nearly as well. To understand the issues in this chapter is to understand when ORM is an appropriate solution and how to use it preserving the strengths of both the OO and the relational database worlds.
The following chapters gradually introduce us to Hibernate's features beginning with simpler examples and moving on to mapping of classes, retrieving and saving object instances, using transaction and caching support, applying Hibernate's query language (HQL), creating reporing queries, and so on.
Throughout the book there is lots of advice on how to avoid performance issues such as the n+1 select problem. Also towards the end there is a chapter demonstrating how to write a layered application and how to handle issues arising from use of a servlet engine, an EJB container, or the use of legacy schemas. The last chapter of the book covers tools for autogeneration of mapping data, schemas, and Java POJOs.
The book is somewhat out-of-date so I recommend catching up on what's new in version 3.x directly on Hibernate's website after reading it. However for the most part the book is valid minus a few API changes and support for new features such as the Java Persistance API.
Last but not least the book comes with the source code of a sample application for online bidding. The application has been kept up-to-date over time, which makes it a valuable learning tool. Also, as Hibernate is open-source software you can download and learn from its large number of unit tests.
Looking back on the project I did using TopLink and the difficulties we had from trying to bridge the OO and the relational world, I think Hibernate successfully addresses these issues. What's more they're summarized quite well in this book and are the foundation of many design choices that have been made in creating Hibernate. However, using Hibernate does not guarantee success and you will need to make your own design choices when building applications with it.