Java is not a language with training wheels. It is a language for professional programmers.

— Hebert Sheildt


Hibernate is an open source, lightweight ORM (Object Relational Mapping) tool / framework. ORM means mapping a class with the table and the respective fields with the database columns.

Hibernate is a framework which is built on top of JDBC, it internally uses JDBC but it makes the programmer to write application specific code and not the common code (like creating registering driver class, creating Connection object, closing Connection object, etc)

Database Table       -- Java Class
Database Columns -- Fields (member variables)

There are several ORM tools available but the popular ones are Hibernate, iBatis, JPA, etc.

The several advantages of Hibernate are (these are few):

1. Hibernate is Database independent.

2. Provides built in Connection Pool (Proxool and C3P0 connection pools).

3. For bulk select operations, it provides results in the form of Serializable objects (Collection), so that it can be sent over the network without any processing.

4. It throws unchecked exceptions, so that we don’t have to handle exceptions while developing the persistence logic.

5. Provides HQL (Hibernate Query Language) and also support native SQL.

Hibernate provides extensive support for annotations for configuring, although I will develop this example using an xml file for configuration purpose. I will be using MySQL as my database s/w so I will also have to add the respective driver jar file.

1. Download Hibernate 3.x from the below link and then extract the zip file.
Hibernate 3.x

2. Add the following *.jar files to the CLASSPATH.
How To

mysql-connector-java-5.1.6-bin.jar (for MySQL)

The resource files required to develop this simple hibernate application are:

a. Configuration File (XML).

b. Mapping File (XML).

c. Persistence Class (POJO Class/ Java Bean).

Configuration File

This file provides the configuration for the Database s/w to be used.

Mapping File

This file maps the table with the class and the columns with fields, also the relationship amongst the tables (1 to 1, 1 to M, M to 1, M to M).

Persistence Class

This is a simple Java Bean, having getter and setter methods for the columns of the table.

SessionFactory is a class that is immutable and is specific to one database. It provides the Session object (not HttpSession) which allows us to communicate us with the database s/w.

Session is a mutable object, this wraps a JDBC Connection object, and Hibernate internally uses a PreparedStatement object.



This is a JavaBean class having getter and setter methods for the fields representing the names of the columns in the table.

The PreparedStatement generated by the Hibernate framework.

Here I use a GUI tool named SQLyog (Community edition i.e free) to interact with MySQL database. You can get it from here.

Here we can see Hibernate inserted the record in the database, this piece of code is applicable to any Database s/w, all we need to make changes to the configuration file.Also notice how Hibernate inserted the date in accordance to the Database s/w used.

Hibernate can also be integrated with other technologies/frameworks like Struts, Spring, JSF, EJB, etc.

© Copyright © 2013 All Rights Reserved.