02 - Hibernate Architecture

2.1 Overview

As we discussed in the earlier chapter, Hibernate is an Object Relational mapping and sits between business (Java) objects) and Database. Hibernate is a framework and has a layered architecture. In this chapter, we will discuss the Hibernate architecture and its underlying components.

Hibernate persists Java object in the database but Hibernate needs  to know about

  1. Database details.
  2.  Metadata of objects like which table to be used, column and data types etc.

These details can be provided either programmatically or using configuration files. Database level details can be configured using properties files (known as hibernate.properties file) or using xml files (known as hibernate. cfg. xml) where as each object requires one configuration file (also known as hbm files).

Refer below diagram to get a very high level framework overview.

                                            fig - Hibernate Framework

                                 Figure - High level view of Hibernate framework 


2.2 Detailed View

Let’s have a  detailed view of Hibernate Architecture. Hibernate is built on top of several Java based API like JNDI, JTA, JDBC etc.

                              Fig - Detailed view of Hibernate Architecture

a. Configuration Object 

Configuration object is created to load the configuration files like hibernate.proerties, hibernatate.cfg.xml and hbm files (Java  objects to database mapping). Since this step is loading of configurations, it generally happens at the application initialization time. 

We can create a configuration object as shown below

          Configuration configuration = new Configuration().configure();

New Configuration() call will load the hibernate.properties fileand calling configure() method on configuration object loads hibernate.cfg.xml. In case any property is defined in both hibernate.properties and hibernate. cfg. xml file, then xml file will get precedence.

To load the custom files , we can call

           new Configuration().configure("/configurations/myConfiguration.cfg.xml")

The above code snippet will load myConfiguration.cfg.xml  from the  configuration subdirectory of the class path.

Hibernate does provide an option to load the configurations programmatically as well. To load hbm files we can use addClass()or addResource() method on the configuration object.Database configurations can be loaded using setProperties() method call.

b. Session Factory 

Session Factory can be obtained from Configuration object and is a heavyweight object and can be used with multiple threads as, the session factory object is thread safe.

SessionFactory class is available under org.hibernate.SessionFactory package.

We would always need one instance of Session factory per database that our application is interacting with. So if we have two different databases, we would create two session factory objects.

As Session factory is a heavy weight object, creation of the session factory object is an expensive operation and recommended to get it created at application start up or can be accessed using JNDI.

Use below code snippet to get a Session Factory object.

    Configuration configuration = new Configuration().configure();

    SessionFactory sessionFactory = configuration.buildSessionFactory();

Once the session factory object is obtained, configuration object is no longer required.

c. Sessions

Session objects are created using Session factory and are a lightweight object. Session objects provide a connection with a relational database.

By design, we should create a new session object when a database interaction is needed. A session object represents a unit of work.

Session objects are not thread safe and hence should not keep open for a long time.

Use below code snippet to open a new Session object

             Configuration configuration = new Configuration().configure();

             SessionFactory sessionFactory = configuration.buildSessionFactory();

  Session session = sessionFactory.openSession();   

  Use session.close() to close the session.

We can pass an optional connection object as an argument to openSession() method if we would want to interact with the database using associated connection. However, this approach is not recommended.  

d. Hibernate Persistent Classes 

Classes whose instances are required to persist in database are known as persistence classes. Any instance of persistent class can be in any one of the below three state.

  1. Persistent Object -  Persistent instances are the objects that are currently mapped with a  session and has a corresponding row in a relational database.
  2. Transient Objects-  Transient instances are the objects which are never associated with a session or with a persistence context.
  3. Detached Objects-  Detached instances are the objects that are mapped to the session or persistent context but session is closed later. In other word , objects whose mapped session is closed.

e. Transactions  

A transaction is a unit of work in which either all operations must execute or none of them.  The transaction is a short lived and a single threaded object.

Transaction is available under org.hibernate.Transaction package

There are four important terms related to transactions are –

  • Atomic -  As described above, atomicity makes sure that either all operations within a transaction must be successful or none of them.
  • Consistent - This property makes sure that data should be in consistent state once the transaction is completed.
  • Isolated-  this property allows multiple users to access the same set of data and each user’s processing should be isolated from others.
  • Durable – Result of the transaction should be permanent once the transaction is completed to avoid any loss of data.

Like us on Facebook