Table of contents
1.
Introduction
2.
Hibernate Architecture Diagram
3.
Configuration
3.1.
Demo Code
4.
Session Factory
5.
Transaction
5.1.
Demo Code
6.
Query
7.
Criteria
8.
Frequently Asked Questions
8.1.
How many layers are there in Hibernate architecture?
8.2.
Describe the layout of Hibernate Architecture?
8.3.
Is Session thread safe in Hibernate?
8.4.
What is lazy loading in Hibernate Architecture?
9.
Conclusion
Last Updated: Mar 27, 2024

Hibernate Architecture

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hibernate framework is a java framework which is used to develop persistence logic. Persistence logic refers to the storage and processing of data for long-term use. Hibernate is an open-source, non-invasive, light-weight Java ORM (Object-relational mapping) framework used to create objects that are independent of the database software and implement independent persistence logic in all JAVA, JEE. Please refer to Hibernate Interview Questions to gain more knowledge about the Hibernate framework.

In this blog, however, we will discuss the hibernate architecture that enables persistence logic development independent of the underlying database software. Hibernate architecture has a layered layout as shown in the diagram below.

Hibernate Architecture Diagram

Hibernate architecture is made up of hibernate core components that make use of existing Java APIs. It communicates with relational databases via JDBC API, and it integrates hibernate with Java application servers using JNDI and JTA. To construct persistence logic in JDBC, we use primitive types. In contrast, we employ Objects to construct persistence logic in Hibernate Framework which enables independence from the underlying database architecture.

Configuration

In any Hibernate application, the first Hibernate object you create is the Configuration object. The Configuration class, which is a part of org.hibernate.cfg package, activates hibernate framework and reads configuration and mapping files.

The Configuration object includes two essential components.

  • Database Connection: Hibernate handles this through one or more configuration files. These are called hibernate.properties and hibernate.cfg.xml files.
  • Class Mapping Setup: This component establishes the link between Java classes and database tables.

Demo Code

Configuration cfg=new Configuration(); // activate Hibernate framework

cfg.configure(); // Read config and mapping files, throw errors if syntactically wrong

Session Factory

A Session is used to establish a physical link with a database. The Session object is lightweight and intended to be instantiated each time a database interaction is required. A Session object is used to save and retrieve persistent objects and perform CRUD operations.

Because session objects are not normally thread safe, they should not be kept open for lengthy periods of time and should be generated and removed as needed.

Transaction

A transaction is a unit of work with the database that is supported by the majority of RDBMS. Hibernate's transactions are handled by an underlying transaction manager and transaction manager (from JDBC or JTA). By invoking the commit() function, the transaction object instructs the database to make the changes that occur as a result of the operation permanent.

This object is optional, and Hibernate applications may choose not to use it, instead managing transactions in their own application code.

Demo Code

Session session=factory.buildSession(); // Create the session.

Transaction tx=session.beginTransaction(); // Begin transaction.

tx.commit(); // Commit changes to the database.

Query

A Query instance is obtained by invoking the Session.createQuery() function.

Query objects retrieve data from the database and construct objects using SQL or Hibernate Query Language (HQL) strings. A Query instance is used to bind query parameters, limit the number of results returned by the query, and execute the query.

Criteria

The Session object is used to build the Criteria object. It's used to create and run object-oriented criterion queries and retrieve entities by assembling Criterion objects.

The Session is a Criteria factory. The factory methods on Restrictions are typically used to obtain criterion instances.

Frequently Asked Questions

How many layers are there in Hibernate architecture?

Hibernate's architecture is divided into four layers. Hibernate's high-level architecture, complete with mapping and configuration files. The Hibernate framework makes extensive use of objects such as session factory, session, transaction, and so on.

Describe the layout of Hibernate Architecture?

Hibernate offers a layered architecture that allows users to work without knowing the underlying APIs. Hibernate uses the database and configuration data to offer the application with persistence services (and persistent objects).

Is Session thread safe in Hibernate?

No, Session object is not thread-safe in Hibernate and intended to be used with-in single thread in the application.

What is lazy loading in Hibernate Architecture?

The Lazy setting determines whether child items are loaded while the Parent Object is loaded. You must achieve this by modifying the parent class's hibernate mapping file. Lazy = True (means not to load child) The sluggish loading of child objects is enabled by default.

Conclusion

Cheers on completing the blog! This blog discussed the hibernate architecture in detail and discussed the objectives of various core components of the hibernate framework.

If you want to explore more blogs on this topic, please follow these blogs especially curated for readers like you - JDBCJDBC ConnectionGuide to ORMSpring Boot JDBC.

Recommended Reading,  Data Warehouse Architecture

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass