Introduction to Hibernate Framework

Hibernate was started by Gavin King in 2001 with a motive to simplify the persistence capabilities offered by EJB2. It simplified the process by adding the features that were missing in EJB2.

Hibernate is an ORM tool. ORM is the Object-Relational Mapping of tables to the entity classes, rows to the class instances and columns of the table to attributes of the instances.

Hibernate is a persistence framework that makes use of a Hibernate cache to persist the data from the Java environment to the database. The Hibernate cache helps in reducing the number of calls made to the database. Entities (Table values) are stored in the Hibernate cache, which contain the updated values.
Hibernate FrameworkHibernate uses persistent objects (POJO) and XML documents to persist objects to the database.


There are three ways by which we can define the configuration (in order):

  1. Properties file (hibernate.properties)
  2. XML configuration file (may be as hibernate.cfg.xml or hibernateContext.xml as possibly in a Spring based application)
  3. Programming

Configuration file (hibernate.cfg.xml) looks something like:

Assume we are dealing with some Account Object as:

Hibernate Object DB table

Object Mapping File (Account.hbm.xml) looks something like:

Entities in Hibernate can be in one of four states:

Hibernate Entity States

  1. Transient – A newly created object.
  2. Persistent – The only state that has impact on the database (auto saved)
  3. Removed – When delete() is called.
  4. Detached – When clear()/ evict() is called; when persistent object is referenced after closure of the session.

Session methods do not make changes to the database; rather they SCHEDULE the changes that are to be made. Once the session is flushed (as per the flush mode), the values are updated in the database.

There are 4 modes for Flushing:

  1. ALWAYS Flushing is done for every query
  2. AUTO – Hibernate controls flushing making sure updated values are always available.
  3. MANUAL – Flushing is specified explicitly.
  4. COMMIT – Flushing is done on encountering commit statements.

A persistence context is a concept that is a list of SQL statements to be sent to the database. Each Hibernate session has one persistence context.

Once configuration is done, a session factory is required to create a session. While SessionFactory is thread-safe (singleton for the entire application), Session is not. A session in Hibernate represents a conversation between the application and the database, is a factory for Transaction and holds a cache of persistent objects.

Now we know what Hibernate is, how to configure it, the different Hibernate states as well as the different flushing modes. For any queries, feels free to post them as comments.

Useful links:

Caching in Hibernate – First and second level cache

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.