Understanding Hibernate ORM for Java/J2EE

Understanding the Hibernate Object/Relational Mapping Solution


As you all know, in today's enterprise environments working with object-oriented software and a relational database can be cumbersome and time consuming. Typically, in an enterprise application, if you are passing objects around and sometimes reach the point where you want to persist them, you will open a JDBC connection, create an SQL statement and copy all your property values over to the PreparedStatement or into the SQL string you are building. This may be easy for a small value object, but for an object with many properties, you may face many difficulties. As myself have experienced and most of you Java Programmers have seen, the object-relational gap quickly becomes very wide if you have large object models. Thus, the activities involved in persisting data are tedious and error-prone. So, we go for Object-relational mapping (O/R mapping) which is a common requirement of many software development projects.

If you are working with object-orientated programming and relational databases, you will surely have noticed that these are two different paradigms. The relational model deals with relations, tuples, and sets—it is very mathematical by nature. The object-orientated paradigm however deals with objects, their atributes and associations to each other. As soon as you want to make objects persistent using a relational database you will notice: There is a rift between these two paradigms, the so-called object-relational gap. A object-relational mapper (ORM) will help you bridge that gap.

If we also take into consideration the inevitable change of requirements, we're in serious trouble: The data storage structure must be kept in sync with the source code. By using ORM, we can make the application portable to all databases.

Why ORM?

The term object/relational mapping (ORM) refers to the technique of mapping a data representation from an object model to a relational data model with a SQL-based schema. So, what can an ORM do for you? A ORM basically intends to takes most of that burden of your shoulder. With a good ORM, you have to define the way you map your classes to tables once; which property maps to which column, which class to which table, and so forth.

With a good ORM, you can take the plain Java objects you use in the application and tell the ORM to persist them. This will automatically generate all the SQL needed to store the object. An ORM allows you to load your objects just as easily: A good ORM will feature a query language too. The main features include:

  1. Less error-prone code
  2. Optimized performance all the time
  3. Solves portability issues
  4. Reduce development time


Hibernate is in my opinion the most popular and most complete open source object/relational mapping solution for Java environments. Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities and can significantly reduce development time; otherwise, spent with manual data handling in SQL and JDBC. It manages the database and the mapping between the database and the objects.

Hibernate's goal is to relieve the developer from 95 percent of common data persistence related programming tasks. Hibernate adapts to your development process, no matter if you start with a design from scratch or work with a legacy database.

Hibernate generates SQL for you, relieves you from manual result set handling and object conversion, and keeps your application portable to all SQL databases. Hibernate allows you to store, fetch ,update and delete any kind of objects. Hibernate lets you develop persistent classes following common Java idiom—including association, inheritance, polymorphism, composition, and the Java collections framework.

The Hibernate Query Language, designed as a minimal object-oriented extension to SQL, provides an elegant bridge between the object and relational worlds.

Key features include:

  1. Integrates elegantly with all popular J2EE application servers, Web containers and in standalone applications. Hibernate is typically used in Java Swing applications, Java Servlet-based applications, or J2EE applications using EJB session beans.
  2. Free/open source. Hibernate is licensed under the LGPL (Lesser GNU PublicLicense).critical component of the JBoss Enterprise Middleware System (JEMS) suite of products.
  3. Natural programming model. Hibernate supports natural OO idiom; inheritance, polymorphism, composition, and the Java collections framework.
  4. Extreme scalability. Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster.
  5. The query language. Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again.
  6. EJB 3.0. Implements the persistence API and query language defined by EJB 3.0 persistence.

What Is a Persistent Class?

Hibernate provides transparent persistence; the only requirement for a persistent class is a no-argument constructor. In a persistent class, no interfaces have to be implemented and no persistent superclass has to be extended. The Persistent class can be used outside the persistence context.

Persistent classes are classes in an application that implement the entities of the business problem. Let me illustate this with a simple eg : login entity. The persistent class can be like:

public class Login {
   /** persistent fields */
   private String userName;
   private String userPassword;

   /** default constructor */
   public Login () {

   public String getUserName() {
      return this.userName;

   public void setUserName(String userName) {
      this.userName = userName;

   public String getUserPassword() {
      return this.userPassword;

   public void setUserPassword(String userPassword) {
      this.userPassword = userPassword;


Persistent classes have, as the name implies, transient and also persistent instance stored in the database. Hibernate makes use of persistent objects commonly called as POJO (Plain Old Java Object) programming model along with XML mapping documents for persisting objects to the database layer. POJO refers to a normal Java object that does not serve any other special role or implement any special interfaces of any of the Java frameworks like a Java Bean.

The Ultimate Goal

Take advantage of those things that relational databases do well, without leaving the Java language of objects/classes. I will say, the ultimate aim is: Do less work, Happy DBA.

Hibernate Architecture

I believe Hibernate provides persistence as a service, rather than as a framework. I will show two common architectures incorporating Hibernate as a persistence layer. As I have already explained, for persisting objects Hibernate makes use of persistent objects commonly called as POJO, along with XML mapping documents.

In Web (two-tiered) Architecture, Hibernate may be used to persist JavaBeans used by servlets/JSPs in a Model/View/Controller architecture.

In Enterprise (three-tiered), Architecture Hibernate may be used by a Session EJB that manipulates persistent objects.

Typical Hibernate code

sessionFactory  = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx  = session.beginTransaction();

//Data access code


The first step in a Hibernate application is to retrieve Hibernate Session. Hibernate Session is the main runtime interface between a Java application and Hibernate. SessionFactory allows applications to create a Hibernate session by reading the Hibernate configuration file hibernate.cfg.xml. After specifying transaction boundaries, the application can make use of persistent Java objects and use the session to persist to the databases.

Getting Started with Hibernate

  1. Download and install Hibernate. Hibernate is available for download at http://www.hibernate.org/.
  2. Include the hibernate.jar file in the working directory.
  3. Place your JDBC driver jar file in the lib directory.
  4. Edit the Hibernate configuration files, specifying values for your database. (Hibernate will create a schema automatically.)

Understanding Hibernate ORM for Java/J2EE

A Simple Java Application

Here is a sample application that I developed; it uses Hibernate.

1. Preparing the Database

First, I created a single table as USER.

USER_ID          <PK>   int(11)
USER_NAME               varchar(20)
USER_ PASSWORD          varchar(10)
USER_ EMAIL             varchar(20)

2. Creating persistent Java objects

The following code sample represents a Java object structure that represents the User table. Generally, these domain objects contain only getter and setter methods.

Source code for User.java

public class User {
   /** identifier field */
   private Long id;

   /** persistent fields */
   private String userName;
   private String userPassword;
   private String userEmail;

   /** default constructor */
   public User() {

   public Long getId() {
      return this.id;

   public void setId(Long id) {
      this.id = id;

   public String getUserName() {
      return this.userName;

   public void setUserName(String userName) {
      this.userName = userName;

   public String getUserPassword() {
      return this.userPassword;

   public void setUserPassword(String userPassword) {
      this.userPassword = userPassword;

   public String getUserEmail() {
      return this.userEmail;

   public void setUserEmail(String userEmail) {
      this.userEmail = userEmail;


Hibernate works best with the Plain Old Java Objects programming model for persistent classes. Hibernate is not restricted in its usage of property types; all Java JDK types and primitives (such as String, char, and Date) can be mapped, including classes from the Java collections framework. You can map them as values, collections of values, or associations to other entities. The ID is a special property that represents the database identifer (primary key) of that class.

3. Mapping POJO with persistence layer using the Hibernate mapping document

Each persistent class needs to be mapped with its configuration file. The following code represents a Hibernate mapping file for the User class.

Source code for User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

   <class name="User" table="user">
      <id column="USER_ID" name="id" type="java.lang.Long">
         <generator class="increment"/>

      <property column="USER_NAME"
      <property column="USER_PASSWORD"
      <property column="USER_EMAIL"

Hibernate mapping documents are straightforward. I will describe the major elements in the mapping file.

1. <hibernate-mapping> element

The root element of the Hibernate mapping document is the <hibernate-mapping> element. This element has several optional attributes.

2. <class> element

The <Class> element maps the domain object with corresponding entity in the database. In simple words, the <class> element maps a table with the corresponding class. The <hibernate-mapping> element allows you to nest several persistent <class> mappings, as shown above. It is, however, good practice to map only a single persistent class in one mapping file and name it after the persistent superclass; for example, User.hbm.xml.

3. <id> element

The <id> element defines the mapping from that property to the primary key column. The <id> element represents the primary key column and its associated attribute in the domain object. Mapped classes must declare the primary key column of the database table. Most classes also will have a JavaBeans-style property holding the unique identifier of an instance.

4. <generator> element

The optional <generator> child element names a Java class used to generate unique identifiers for instances of the persistent class. If any parameters are required to configure or initialize the generator instance, they are passed using the <param> element. Some commonly used generators are:

  1. Increment. Generates identifiers of type long, short, or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster.
  2. Sequence. Uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, or a generator in Interbase. The returned identifier is of type long, short, or int.
  3. Assigned. Lets the application assign an identifier to the object before save() is called. This is the default strategy if no <generator> element is specified.
  4. Foreign. Uses the identifier of another associated object. Usually used in conjunction with a <one-to-one> primary key association.
5. <property> element

The <property> element declares a persistent, JavaBean style property of the class. The <property> elements represent all other attributes available in the domain object.

typename could be:

  1. The name of a Hibernate basic type (integer, string, character, date, timestamp, float, binary, serializable, object, or blob).
  2. The name of a Java class with a default basic type (int, float, char, java.lang.String, java.util.Date, java.lang.Integer, or java.sql.Clob).
  3. The name of a serializable Java class.
6. <many-to-one> element

An ordinary association to another persistent class is declared using a many-to-one element. The relational model is a many-to-one association: A foreign key in one table is referencing the primary key column(s) of the target table. A typical many-to-one declaration looks like this:

<many-to-one name="product" class="Product" column="PRODUCT_ID"/>
7. <one-to-one> element

A one-to-one association to another persistent class is declared by using a one-to-one element. A typical many-to-one declaration looks like this:

<one-to-one name="product" class="Product" column="PRODUCT_ID"/>

4. Hibernate configuration file

The Hibernate configuration file contains information needed to connect to the persistent layer and the linked mapping documents. You can either specify the data source name or JDBC details that are required for Hibernate to make a JDBC connection to the database.

The element <mapping-resource> refers to the mapping document that contains mapping for the domain object and Hibernate mapping document.

Source code for hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD//EN"


      <!-- Database connection settings -->
      <property name="show_sql">true</property>
      <property name="hibernate.dialect">
      <property name="hibernate.connection.driver_class">
      <property name="hibernate.connection.url">
      <property name="hibernate.connection.username">
      <property name="hibernate.connection.password">

      <!-- Mapping files -->
      <mapping resource="User.hbm.xml"/>

Here is how you can use Hibernate in your programs. Typical Hibernate programs begin with a configuration that is required for Hibernate. Hibernate can be configured in two ways: programmatically and configuration file-based. In configuration file-based mode, Hibernate looks for the configuration file b.hibernate.cfg.xmlb. in the claspath. Based on the resource mapping provided, Hibernate creates a mapping of tables and domain objects. In the programmatic configuration method, the details such as JDBC connection details and resource mapping details are supplied in the program by using the Configuration API.

An example of a programmatic configuration of Hibernate:

Configuration config = new Configuration().addResource(b.User.hbm.xml");
Configuration config = new Configuration().addClass(User.class)
                "org.hibernate.dialect. MySQLDialect")
                " com.mysql.jdbc.Driver")
    SessionFactory sessions = config.buildSessionFactory();

Understanding Hibernate ORM for Java/J2EE

4. Inserting new records

Source code for UserInsertHibernate.java

import org.hibernate.*;
import org.hibernate.cfg.*;

   public class UserInsertHibernate {
      public static void main(String[] args)
         throws Exception {

         // Fire up Hibernate
         SessionFactory sessionFactory = new Configuration()

         // Open Session
         Session session = sessionFactory.openSession();

         // Build a User
         User user = new User();
         Transaction tx = session.beginTransaction();

         // Save User and close Session


4. Quering the database

Source code for UserQueryHibernate.java

import java.util.Iterator;
import org.hibernate.*;
import org.hibernate.cfg.*;

   public class UserQueryHibernate {
      public static void main(String[] args)
         throws Exception {

         //Fire up Hibernate
         SessionFactory sessionFactory = new Configuration()

         //Open Session
         Session session = sessionFactory.openSession(); 

         //Query using Hibernate Query Language 
         String SQL_STRING = "  FROM User as users";
         Query query = session.createQuery(SQL_STRING);
         System.out.println("aftr createQuery" );
         for (Iterator it = query.iterate(); it.hasNext();) {
            User user = (User) it.next();
            System.out.println("User name " + user.getUserName() );
         System.out.println("User Email " + user.getUserEmail() );

      //Close Session


Hibernate Query Language (HQL)

Hibernate is equipped with an extremely powerful query language that looks very much like SQL. Queries are case-insensitive, except for names of Java classes and properties.

HQL is a language for talking about "sets of objects." It unifies relational operations with object models. It makes SQL be object oriented. It uses classes and properties instead of tables and columns. It supports polymorphism, associations, and is much less verbose than SQL.

Other features include:

  1. Full support for relational operations
  2. Inner/outer/full joins, cartesian products
  3. Projection
  4. Aggregation (max, avg) and grouping
  5. Ordering
  6. Subqueries
  7. SQL function calls

Simplest HQL Query:

From User
      - Returns all instances of the class User

From User as user, Group as group
      - using aliases

More realistic example:

Select user.name from User user where user.name like 'mary%'

Alternative Persistence Frameworks

Some other persistence frameworks are:

Open Source ones are:

  • OJB
  • iBATIS
  • Castor

Commercial ones are

  • JDO Genie
  • Jrelay

This article was originally published on July 12th, 2005

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date