Architecture of Hibernate

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();


Related Topics

Hibernate Merge

Hibernate Merge Merge is a method available in Hibernate which is used to update the existing records. However, it creates a copy from the passed entity objects and returns it. In other words, the merge()...

2 minutes read.

Hibernate History and Versions

Next → History of Hibernate Hibernate was developed in 2001 by Gavin king with his colleagues from Circus Technologies. The main aim was to provide better persistence capabilities than those of EJB2, by reducing the...

2 minutes read.

Architecture of Hibernate

Next → 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...

2 minutes read.

Hibernate First-Level Cache

The first-level cache is related to the Session object. First level cache objects of one Session are not visible to the other Sessions, and all the cache data is lost when the Session...

2 minutes read.

Hibernate Mapping

Hibernate Mapping Association- It means that two or more things are related to each other by some specific relation. In other words, it specifies how the objects are associated with each other. Hibernate mapping is one...

2 minutes read.

Hibernate first Example

Now we are going to create the first example in Hibernate. We will proceed step by step to develop the first Java application in Hibernate. The steps are listed below:- Create the POJO...

2 minutes read.

Hibernate Web Application with Example

Let us create a hibernate web Application. Here we need JSP file for presentation. There are steps given below for the web app creation. index.jsp This page will show a registration form. It takes the...

2 minutes read.

Hibernate Web Application Example

Web Application Example with Hibernate In this section, we create a hibernate web Application. Here we are using a JSP file for presentation. Example of creating a web application using hibernate index.jsp This page will show a...

2 minutes read.

Hibernate Inverse

Hibernate Inverse An Inverse attribute is used to maintain the relationship between the parent and child class object. The inverse attribute is used only with bi-directional mappings such as one-to-many and many-to-many Hibernate mapping. In Hibernate, the...

2 minutes read.

Hibernate One-to-Many Mapping

One-to-Many Hibernate Mapping Example In One-to-many association mapping, only one object of a persistent class is related to many objects of another persistent class. It is a relationship in which one (parent) is related...

3 minutes read.

Hibernate Many-to-Many Mapping

Many-to-Many Hibernate Mapping with Example In Many-to-Many association mapping, more than one objects of a persistent class are associated with more than one objects of another persistent class. For example, many (categories) are related to...

3 minutes read.

Hibernate Tutorial

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

3 minutes read.

Hibernate Named Query using Annotation

In annotation-based named query, we use @NamedQuery annotation inside the POJO class. @NamedQuery- It is used to describe a single named query. It consists of four attributes- two of which are compulsory, and...

2 minutes read.

Hibernate Named Query Using XML

A named query is a technique used to assemble all the queries (Native SQL and HQL) in a specific location and refer them by some name. It helps in reducing the mess...

3 minutes read.

Hibernate N+1 Select Problem

Hibernate N + 1 Select Problem The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number...

4 minutes read.

Hibernate vs. JDBC

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

2 minutes read.

Hibernate merge() vs. update()

In Hibernate, both update() and merge() methods are used to convert the detached state object into the persistent state. session.merge() The merge() method is used when we want to change a detached entity into...

2 minutes read.

Hibernate Caching

What is Caching? Caching is a process of saving data into the cache memory. A cache is a temporary storage layer which is used to increase the speed of data access. It allows...

2 minutes read.

Hibernate Session Interface

Hibernate Session Interface A Session is an interface between the Java application and the Hibernate. It is available in the org.hibernate.session package. It should not be kept open for a long time, as it...

4 minutes read.

Hibernate Query Language (HQL)

Hibernate provides its query language called Hibernate Query Language (HQL). HQL is an object-oriented query language, similar to the native SQL language, and it works with persistent objects. It is a database-independent and...

2 minutes read.