Introduction and Architecture of Hibernate 4

Posted By Nagesh Chauhaan | 26-Nov-2012

In this series of blogs we are going to look at , what Hibernate is , how we can use Hibernate in our applications and what are the features that Hibernate provides .

Hibernate was initiated by Gavin King in 2001. When first started  it was something called as an ORM Tool , but today Hibernate is actually much more than an ORM Tool. There are a lot of features that have been added to it over the years. Today Hibernate is known as the best and dominated object/relational persistence (ORM) tool for Java developers (Now is support .NET). Hibernate is primarily used in Data Layer to persist your application data into the Database.

Persistence is a storing  process of data to permanent storing medium like SQL database and fetching back to any time which may be after ending the process which stores data to this table.

Hibernate implements JPA(Java persistence API)  that makes it world wide acceptable .

JPA implementation means that it follows the rules that have been specified in Java Persistence API’s specifications so that later if you don’t want to use Hibernate you can use some other provider that implements Java Persistence API and that can be done with a minimal code changes.


We w’ll look at features and functioning of Hibernate in some subsequent blogs , before we start with creating an hibernate application lets first understand what an ORM Tool is ? and why is it required.


Hibernate Over JDBC

Let’s take a look at what are the problems that Hibernate solves.

In upcoming blogs we are going to talk about the Java Applications and Databases and most importantly the relations between them.

Before we start , let’s review the common functionality of each data driven application. Lets assume we have a simple class in our application say Employee Class.

In our running application we will use this objects in memory to manage the employee entity. We w’ll have one instance of this class per user and that perticular object w’ll represent the indivisual employee in the application.

Now lets say we want to persist the employee data , for doing this we w’ll use a database and 99% of the time we w’ll be using the Relational Database. Now to persist employee’s data ,we w’ll create a  table in Database. The table for Employee entity w’ll look something like this .

In the table we have five columns that corresponds to five member variable of Employee Class. Depending upon the number of objects, we would have correspond number of row in the table.

Here we can see that a Class corresponds to a Table and an object of class corresponds to the row in Table. And this is all we are doing over the years.

In order to save the data , we would create a Jdbc Connection to connect the java application to the database. Now we would take the object and pull all the fields with the help of a SQL query that would do an insert in the Database.

In the same way if we need to create an employee’s object , we would do a select on this table to get a recordset , we would pass this recordset and pull the indivisual data. And this is all we does with a jdbc approach.


So the problem here is this “we have objects in java but we don’t have objects in database level” .

Java Class is an individual entity that needs to be converted in order to save it in Database. Normaly we does this with the help of some code that establish a mapping in between these two entities . Now this mapping is a great pain .


Pain points with JDBC

  1. Mapping member variables to columns: Each and every member variable that we have to be persist should be mapped manually. So if we added an extra field in our class than we have to changes our full code to make the changes in Database.
  2. Mapping Relationships : Lets say our Employee Class have a relationship with Salary Class, to make it working we added some primary and foreign keys .So this relationship also has to be taken care .
  3. Handling Data types : Now if we have an boolean in our Object class say if a user is Active or Inactive , to do this we give true of false value in our Employee class, but most of the databases do not have Boolean data type. So we need to handle this data type conversion  in our code manually .
  4. Managing changes to object state : Again for doing this we w’ll have to use another Update query and sometimes trigger and procedures . So we need to frame the mapping again .

And much more but, these are main pain points that we need to handle in each and every application.


Now we come to know that the things that are happening in Relational side are completely different from the things that are happening in Object Oriented side and there is a huge gap in between these two.

We can not  change the relational side of doing things , therefore we are forced  to manage the things in OOPS side of it , and this is what Hibernate does for us . Hibernate provides an appropriate and effective way to reduce the gap between Relational Database and OOPS.


So this is all about ORM . Hibernate is a framework that provides such kind of support to make our application fully object oriented .


Architecture and Elements  of Hibernate 4

Hibernate consists of a three layered architecture as shown in picture below.



(1) Java Application layer : In order to persist data to the database an instance of entity class is created This object is called Transient object as they are not yet associated with the session or not yet persisted to a database.

 (2) Hibernate Framework Layer : To save the object to database,a instance of SessionFactory interface is created. SessionFactory loads Hibernate configuration file(hibernate.cfg.xml ) and with the help of TransactionFactory and ConnectionProvider implements all the configuration settings on a database. Each database connection in Hibernate is created by creating an instance of Session interface. Session represents a single connection with database. Session objects are created from SessionFactory object.

 (3) Database layer. Most of the time we deal with a relational database. Data is represented in the form of table and rows.


Elements of Hibernate

  1. SessionFactory  : SessionFactory is actualy an interface that extends Referenceable and. Serializable. Usually an application has a single SessionFactory. Threads servicing client requests obtain Sessions from the factory.
  2. Session : A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.
  3. Transaction : In hibernate A transaction represents a  unit of work. If you want the database to do some work for you, then you need to start a transaction. When you are finished interacting with the database, you end the transaction, and let the database know you are done.
  4. ConnectionProvider : ConnectionProvider is basicaly an Interface that provides connection between the application and database.
  5. TransactionFactory : This is not exposed to the application , but it can be implemented and used by the develop as an option.

Here we are done with a brief introduction of Hibernate and its architecture.

In next blog we w’ll discuss How to make a simple Hibernate Application in Eclipse.





Hope it helps !

Nagesh Chauhan

Request for Proposal

Recaptcha is required.

Sending message..