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

Cascade in Hibernate

Cascading is a feature in Hibernate, which is used to manage the state of the mapped entity whenever the state of its relationship owner (superclass) affected. When the relationship owner (superclass) is saved/...

3 minutes read.

Hibernate Lifecycle

Hibernate Lifecycle In Hibernate, the mapped instances of the entity classes have a lifecycle. Either we can create a new object, or we can fetch the existing data from the database. The value objects do...

3 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 get() vs. load()

Hibernate methods, get() and load() both are provided by the Session interface and used to retrieve the objects from the database. Session.get() The session.get() method is used to fetch a persistent object of 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 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 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 vs. JPA

What is JPA? JPA stands for Java Persistence API, which defines a set of functionalities, standards, and concepts to the ORM tool. It is a framework used to manage relational databases. It is available...

2 minutes read.

Hibernate Inheritance - Single Table

Hibernate Inheritance In this inheritance strategy, only one table is created for all the classes involved in the hierarchy with an additional column known as a discriminator column. The discriminator column helps indifferentiating between...

3 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 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 GeneratedValue Strategies

Hibernate GeneratedValue Strategies Hibernate supports some generation strategies to generate a primary key in the database table. We can access the strategy with the help of @GeneratedValue annotation. @GeneratedValue The @GeneratedValue annotation specifies how to...

3 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 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 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 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 Configuration Properties

Hibernate Configuration Hibernate Configuration is a Java class, which allows a Java application to specify configuration parameters used in the application. As Hibernate is designed to serve in different environments, it needs a broad...

6 minutes read.

Hibernate SessionFactory

Next → Hibernate SessionFactory A SessionFactory is an interface in Hibernate that create the instances of Session interface. It is a heavy weight object, and it is usually created during the application...

3 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 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.