Architected, designed, implemented and lead teams for various size software engineering projects involving near realtime transactions for airlines and healthcare industries. Experience with wide range of technologies (ACP, TPF, ALCS, Unix/Linux, .Net, Java/JEE, etc). Recently involved with data analytics systems for video on demand. Aziz has posted 3 posts at DZone. View Full User Profile

Hibernate Recipes - A Problem-Solution Approach

11.25.2010
| 1810 views |
  • submit to reddit
Published by: APRESS
ISBN: 9781430227960

Reviewer Ratings

Relevance:
4

Readability:
3

Overall:
3

Buy it now

One Minute Bottom Line

This book is for people who have back ground in Java and relational database systems (Oracle, Derby, etc). This tutorial starts from the ground up, teaching all aspects of Hibernate, an Object Relational Mapping framework. It provides instructions on how to setup the environment (e.g., required libraries) to work with Hibernate. It is a down to earth practical book that describes specific problems and solutions thereof with the ORM perspective. It includes usage of annotations for JPA (EJB 3.0).

Review

The book starts with brief intro to object and relational models to highlight the differences between the two. This prepares the reader to understand the advantages of an Object/Relational Mapping (ORM) based framework that bridges the differences between the two models. Then it enumerates the advantages and disadvantages of using ORM. It presents Hibernate (an open source project, a widely used mature ORM) which is one of the implementation that provides an ORM based framework.

The first chapter attempts to describe a commonly understandable system that would be needed by a generic bookshop (Publisher, Book and chapters belonging to a book, etc) automation. It goes through a possible implementation using JDBC. Please, note that it is not a tutorial on JDBC. The purpose of this is to see what steps are involved in implementation using JDBC and the impact (e.g., dependency on the SQL dialect of the DB, too much copy code, manually handling of tables associations).

The book's first chapter also includes brief but specific configuration steps involved for using Hibernate with XML or Hibernate annotations ( EJB 3.0 - JPA based), log4j, database schema generation. If you don't understand the pieces here, don't worry - keep marching on. Next chapters will describe in great detail. This is just a bird eye view! There are other ORM providers so it is good to pay attention on the section that looks at Hibernate Entity Manager (manages lifecycles of entity instances) which provides a generalized mechanism based on JPA specs. This way in future you can swap out Hibernate for other JPA standards based ORM.

The second chapter starts by clarifying the concept of identity of a record in a table namely primary key. Then it describes the mapping for Hibernate id element for XML configuration file and the @id annotation for Java file. It also discusses inheritance mapping (multiple classes mapped to one table). It further discusses the strategies to generate the ID (which could be specific to the DB e.g., Oracle, MySQL). It goes over the solution using composite key as identifier.

In each "How It Works" section for a problem, both JPA(EJB 3.0) and Hibernate XML based solutions are discussed and related code presented.

What if the domain model is fine grained and you have more than one classes that need to be mapped to one row of a table? Chapter 3 is about the different scenarios. It discusses a Value Type (a component) and how you one would implement solutions using XML mapping or JPA annotations. Different scenarios explained here are having nested components, references to components and most importantly collection of components. It also discusses components as keys. Chapter 6 builds upon this introduction and offers great details about different collection mappings (e.g., Set, Bag, List, Array, etc).

Chapter 4 is about principles of object orientation namely inheritance and polymorphism. It discusses the inheritance strategies provided by Hibernate. The tables at the end of the chapter are useful for reference purposes.

The relational and domain model objects have different types of relationships/associations to each other. The complex topic of many-to-one and one-to-one relational mappings are discussed in chapter 5. It sheds light on different ways to use these mappings. And further going on the same theme, we have details of one-to-many and many-to-many relationships in chapter 7. For example, it provides solution for a problem of one-to-many bidirectional association using foreign key or using a join table.

Chapter 8 is all about HQL and JP QL. JP QL is basically a subset of HQL and you should use it to make your code portable. HQL is similar to SQL but 'object oriented' and allows you to code without bothering about underlying database (Hibernate generates the SQL). It discusses parameter binding using position or name of the parameter. What you do when you have to cope with large data rows being returned? Well, this chapter tells you how to use 'pagination' provided by the HQL/JP-QL. The language requires you to think in SQL terms though. So the next chapter 9 is dedicated to explain Hibernate Criteria API which is fully object-oriented.

The discussion about persistent object states (e.g., transient, detached, etc) is in chapter 10. I am not sure if it could be done earlier. Interceptor is also discussed in this chapter.

Chapter 12 is about the 'CACHING' features that Hibernate provides. Understanding of these features is important for performance of applications. This talks about different types of caches and cache providers. And of course, the book will not be complete without the all important discussion about transactions and concurrency which is in chapter 13.

There is something what I would call a bonus chapter, titled 'Web Applications'. The discussion starts with the MVC pattern and goes through the development cycle to create artifacts so the application can be deployed on a webserver (Tomcat). Here you taste the complete meal prepared using from Hibernate Recipes!

A very important Hibernate facility is defining custom mapping types. This should have been a separate chapter on its own but is part of Chapter 4. The chapter gives details of how to implement the Hibernate provided interface and the extension points thereof (userType, UserCollectionType, etc). For example if we have a mutable class with two properties (areaCode and telNo) and the database table has a single column (for the phone number) then, how one would implement the UserType for mapping purposes.

Lazy Initialization is Hibernate's feature for performance/tuning. This is discussed in Chapter 6 as part of collection mapping. There are other performance related items discussed in other chapters. It would be nice if the performance/tuning related problems/solutions were put in a separate chapter for the purpose.

The book needs a little more proof reading. The very obvious example is the title of chapter 12 "Cashing in Hibernate". We know the writers meant it to be 'caching'!

Published at DZone with permission of its author, Aziz Rahman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)