EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Hibernate Architecture

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Java Technology Tutorial » Hibernate Architecture

Hibernate Architecture

Introduction to Hibernate Architecture

Before getting into the Hibernate Architecture, let us look at why Hibernate came into the picture and what purpose it solves for us. Hibernate is an ORM (Object Relational Mapping) tool for a java programming language to develop object-based database-independent persistence logic. Hibernate provides a layer of abstraction for retrieving and storing data in a database.

Why Hibernate?

JDBC (Java Database Connectivity) is database dependent. To be more specific, writing queries to be run from your application using JDBC depends on what database you are working with. If you work with MySQL or Oracle, your queries will be different. So If in the middle of a project you need to change your database that will require a change of code too, and of course we don’t want that. Whereas, hibernate uses a database-independent Hibernate Query Language. In short, hibernate takes care of most of what the developer had to do with JDBC, for eg. Opening a connection to the DB, closing connections, handling various exceptions.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

So let’s get into the Hibernate Architecture to better understand how Hibernate has made data persistence so much easier.

Hibernate Architecture

The main concept being any ORM is to replace writing database-specific SQL queries with manipulating objects.

To put it in simple words,

  • you create a POJO (Plain Old Java Object)
  • assign values to the fields using setters
  • tell hibernate to persists those objects

Hibernate maps Java classes to database tables (which also involves mapping the java data types to SQL data types).

So it takes away the pain of converting the java fields to SQL supported data types. Hibernate does it for you.

It also relieves the developer from the pain of result set handling.

Popular Course in this category
Java Training (40 Courses, 29 Projects, 4 Quizzes)40 Online Courses | 29 Hands-on Projects | 285+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.8 (9,016 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)JavaFX Training (1 Courses)JMeter Testing Training (3 Courses)

Hibernate Architecture

Java Application

While developing the persistence logic we need to take the support of some resources in the java application. Let us look at these resources.

The heart of any Hibernate application lies in its configuration, which is done using,

  • Configuration file
  • Mapping file

Hibernate Configuration File

We define the properties which tell hibernate what database it has to interact with and which JDBC driver to use. Hibernate uses a configuration file to get all this information.

Although Hibernate is capable of generating database queries, we can specify the dialect to help it generate better SQL for the required database.

For e.g. following are the properties you will typically set in a spring boot application

Hibernate Architecture 1

Hibernate Mapping File

It holds all the class-to-table mapping definitions. Every ORM tool needs a mapping file. We use the following ways to tell Hibernate what value of an object has to be stored in which column of the table.

  • XML
  • Annotations

Syntax of an XML mapping file in spring,

Hibernate Architecture 2

Following is how we annotate the instance variable that should be mapped to a column of a row in the database.

instance variable

Domain Class

As we know Hibernate is known for developing object-based persistence logic. So a domain class is a POJO that is mapped to a table in the database.

Configuration

To create a connection to the database, Hibernate needs to know the details of the database, classes and other details. A configuration object is created once when an application is initialized. It is mainly responsible for bootstrapping Hibernate to the application. A configuration object uses the configuration file and the hibernate mapping file to perform the following steps:

  • Load these files.
  • Checks if these files are well-formed and valid.
  • Creates in-memory metadata of these two files.
  • Return an object which contains this metadata.

Configuration

Session Factory

A session factory is used to create session objects. It is created by the configuration object. It provides a layer of abstraction, so we don’t have to worry about how the session object is created. It is a thread-safe global factory class for creating sessions. Using the configuration object, we get access to the in-memory data created from the configuration and mapping file.

From there, it gets the JDBC properties- driver name, data source URL, username, password, etc. Using these properties, it will create connection objects, representing connectivity to the database.

Using these connections, a connection pool is created. All the information from the configuration object and connection pool is used to create and return a session object. Hence, a session factory object is a heavyweight object. Each session factory is configured to work with a specific database using one of the specified Hibernate dialects.

Session

One thing to note is that it is not related to a servlet session or a java mail session. A session is a gateway to our database. It creates a bridge from our application to the database. It is created by the session factory. It takes the connection object and opens a connection/session to the database, and allows the application to perform persistence operations on the database.

In an ORM tool, you perform all operations like INSERT, DELETE, and UPDATE using object-oriented semantics, which means you are no longer referring to tables, rows, and columns. It is the sessions work to do all the persistence operations for you.

Transaction

A transaction is a short-lived object which represents a single unit of work like saving or delete an object in a table in the database. It abstracts the application from the underlying transaction done by using JDBC, JTA (Java Transaction API), etc.

Internally, hibernate using JDBC, JTA, JNDI (Java Naming and Directory Interface) to generate SQL queries for the configured database.

Conclusion

In this article, we looked at how the layered architecture of hibernate helps a programmer to work with a database without having to know the details of the underlying API like JDBC, JTA, and JNDI. We learned how Hibernate makes our life easier by handling most of the boilerplate code like opening/closing connection to the database. Hopefully, this gives you a clear understanding of the Hibernate Architecture, which will get you started on using the Hibernate Framework in your applications.

Recommended Articles

This is a guide to Hibernate Architecture. Here we discussed Hibernate Architecture to better understand how it has made data persistence much easier along with Java Application. You may also look at the following article to learn more –

  1. What is Hibernate?
  2. Hibernate Interview Questions
  3. What is Java Hibernate?
  4. Hibernate Tools

Java Training (40 Courses, 29 Projects, 4 Quizzes)

40 Online Courses

29 Hands-on Projects

285+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
Hibernate Tutorial
  • Basic
    • What is Hibernate
    • What is Java Hibernate
    • Hibernate Versions
    • Hibernate Architecture
    • Hibernate Framework
    • Hibernate Tools
    • Hibernate Annotations
    • Hibernate Mapping
    • Hibernate Many to One
    • Hibernate Many to Many
    • Hibernate Dialect
    • Hibernate Session
    • Hibernate SessionFactory
    • Lazy Loading in Hibernate
    • Hibernate Criteria
    • Hibernate Persist
    • Hibernate EntityManager
    • Caching in Hibernate
    • Cascade in Hibernate
    • Hibernate Generator
    • Hibernate Validator
    • Spring Hibernate Integration
    • Hibernate Query Language
    • Hibernate Named Query
    • Hibernate Interview Questions
    • HQL
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Java Training (40 Courses, 29 Projects, 4 Quizzes) Learn More