Hibernate is an ORM framework with a layered architecture. It consists of many objects, such as a persistent object, sessionfactory object, transaction object, session, etc. 

There are mainly three layers in Hibernate architecture:-

  • Application Layer
  • Hibernate Core Layer
  • Database Layer
hibernate architecture

Core Components of Hibernate

Before the creation of the first Hibernate application, we must know the core components of Hibernate. They are listed below:-

  1. Configuration

The org.hibernate.cfg package contains the Configuration class, which consists of the properties and function files of Hibernate. The configuration object is created only once during the application initialization.

To activate the Hibernate Framework, we use the following code:

Configuration cfg=new Configuration();

It reads both mapping and configuration file.

cfg.configure();

2. SessionFactory

The org.hibernate.sessionFactory package contains the SessionFactory interface whose object can be obtained by the object of Configuration class. It is a threadsafe object and used by all the threads in the application.

The below code shows the creation of SessionFactory object:

SessionFactory factory=cfg.buildSessionFactory();

It takes the JDBC information from cfg object and creates a JDBC connection.

3. Session

The org.hibernate.session package contains the Session interface. A SessionFactory object is used to create a Session object, which is lightweight object. The Session object is not threadsafe. It is used to execute CRUD operations (insert, delete, update, edit). It also holds the first-level cache data in Hibernate.

The below code shows the creation of Session object:

Session session=factory.buildSession();

4. Transaction

The org.hibernate.transaction package contains a Transaction interface. The object of the session creates a Transaction object. It provides the instruction to the database for transaction management. It is a short-lived single-threaded object.

The below code shows the creation of Transaction object:

Transaction t=session.beginTransaction();

t.commit();

The commit() function is used to close the transaction.

5. Query and Criteria

The org.hibernate.query and org.hibernate.criteria package contains Query and Criteria interface, respectively. Query objects use Hibernate Query Language (HQL) to get data from the database. Criteria objects are also used to retrieve data from databases. Session objects are used to create query and criteria objects.

The below code shows the initialization of Query object:

Query query= session.createQuery();

The below code shows the Initialization of criteria object:

Criteria criteria=session.createCriteria();

Pin It on Pinterest

Share This