Hibernate Speedest introduction

In this article I will introduce you to Hibernate 3 persistence framework.
This article is worth to read, because give you a very fast track to Hibernate; I have used this tool for the past six years, when only the good 2.x series was out of there.
Then I have the lucky of training two teams on two different and big projects, with a “zero time” to teach them hibernate.
So I decided to put here my advices, hints, tips and tricks, for the rest of the guys around the World.

The goal of Hibernate “is to relieve the developer from 95 percent of common data persistence related programming tasks”.
Hibernate is an automatic tool to map in an efficient way objects to database

Hibernate is also an integration tool, because it is able to adapt a pre-existing database model to a brand new object model, providing an object-oriented view of a relational database. To cook them all, a set of “database” dialects are used to adapt tightly to the strength of the underlying database.
But you will soon like the hibernate-way :-)

You can think of DataAccessObjects (DAO) as “machine language”.
You have two options: you can write them by hand, or you can use a C compiler to do it (Hibernate is like an interpreter to mimic DAO-like access).

Hibernate is fast, efficient and can be tuned to be also fastest then a DAO written by hand.
You can believe me: you will not be able to be fastest then Hibernate (it is like to try to run faster then a Ferrari GTO using only your feet).

Only when you plan to do a sequential scan of a big table (for example for a batch work) you will come into trouble. But in any other context, Hibernate will outperform your DAOs.

Take a look to the documentation, mostly the good hibernate_reference.pdf.

I suggest you this order:

  • Take a look to the first two chapters (Introduction and Architecture)
  • Take a deep reading of Chapter 5 (Basic O/R Mapping) and Chapter 6, Collection Mapping
  • The Chapter 14 HQL: The Hibernate Query Language, will be used soon by you, so keep an eye on it.
  • Hibernate can generate for you SQL, in a very precise way. Take a look to Chapter 20.
  • Finally  take a look at the missed chapters.

The Hibernate manual is very well written, and you will not need any other guide to get started.

Let’s take a look to the following core concepts:

  • You must understand the reason of redefinition of hashcode and equals, looking at § 4.3 “Implementing equals() and hashCode()”.
  • You must understand why there is a first-level cache for the current session, and how lazy associations works.
    Why lazy association are so important?
  • For simplicity and easy of use,  I suggest you to start without a second level cache adding it only in a second step. Second level cache is a boost, but can also generate side-effects.

Last hint: do not expose Hibernate objects to the front end (like to Struts Action).

Use old plain vanilla data transfer beans (DTB) to communicate with front end layers.This approach is necessary, for avoiding troubles in the future.

If you have querstions, feel free to leave down a comment and I will reply you.


2 thoughts on “Hibernate Speedest introduction

Comments are closed.