Introduction to Hibernate

Reading Time: 3 minutes

In this article, we are going to discuss the Introduction to Hibernate framework.

Before proceeding, we need to understand concepts of Core Java, SQL as well as the working of JDBC. Let’s understand Object Relational Mapping(ORM).

What is ORM(Object Relational Mapping)?

Object-Relational Mapping (ORM) technologies are responsible for saving/reading/editing/deleting the objects from our application to the relational database tables. ORM makes our Java application more flexible by protecting developers from working with SQL Languages. Each relational database has its own standards of SQL language.

ORM

Why it is required?

  • It shields developers from SQL
  • Productivity improves: (Less Java code to write, High-level object-oriented API, No SQL to write)
  • Performance improves: (Sophisticated caching, Lazy and Eager loading)
  • Maintainability improves: (A lot less code to write)
  • Portability improves: (ORM framework generates database-specific SQL)

Hibernate

It is an object-relational mapping (ORM) tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database. Also handles object-relational impedance mismatch problems by replacing direct, persistent database accesses with high-level object handling functions.

Primary feature is mapping from Java classes to database tables and mapping from Java data types to SQL data types. Also provides data query and retrieval facilities. It generates SQL calls and relieves the developer from the manual handling and object conversion of the result set.

Hibernate Position

Hibernate Architecture

Hibernate is an ORM framework with a layered architecture that helps the user to operate without having to know the underlying APIs. It also makes use of the database and configuration data to provide persistence services to the application.

There are basically 3 layers in this architecture:-

  • Application Layer
  • Hibernate Core Layer
  • Database Layer
Hibernate Architecture
Hibernate Application Architecture

Core Components of Hibernate

Let’s have a brief description of each of the class objects involved in this Architecture.

  1. Configuration:
    • org.hibernate.cfg package contains the Configuration class.
    • Configuration cfg = new Configuration() used to activate the Framework.
    • It reads both cfg file and mapping files cfg.configure();
    • Configuration object created once, at the time of initialization.
  2. SessionFactory:
    • org.hibernate.sessionFactory package contains the SessionFactory Interface.
    • It is immutable and thread-safe in nature.
    • SessionFactory factory = cfg.buildSessionFactory() used while creation of SessionFactory object
    • From cfg object it takes the JDBC information and create a JDBC Connection.
  3. Session:
    • org.hibernate.session package contains the Session interface
    • Session object created based upon SessionFactory object i.e. factory.
    • It is a lightweight object and not thread-safe.
    • Session object used to execute CRUD operations.
    • Session session = factory.buildSession() used while the creation of Session object
  4. Transaction:
    • org.hibernate.transaction package contains the Transaction interface
    • Transaction object used whenever we perform any operation and based upon that operation there is some change in the database.
    • It is a short-lived single-threaded object.
    • Transaction tx = session.beginTransaction();
      tx.commit(); used while creation of Transaction object.
  5. Query:
    • org.hibernate.query package contains the Query interface. 
    • The session objects are used to create query objects.
    • Query objects use Hibernate Query Language (HQL) to get data from the database. 
    • Query query = session.createQuery() used while initialisation of Query object.
  6. Criteria:
    • org.hibernate.criteria package contains the Criteria interface.
    • The session objects are used to create Criteria Objects.
    • Criteria criteria = session.createCriteria() used while initialisation of Criteria object.

I hope you liked the blog and was able to understand the brief introduction. Stay tuned for the next part related to annotations and more.

To know more about Hibernate, you can visit this link.

knoldus-blog-footer-banner.jpg?

Written by 

Kuldeep is a Software Consultant at Knoldus Software LLP. He has a sound knowledge of various programming languages like C, C++, Java, MySQL, and various frameworks like Apache Kafka and Spring/Springboot. He is passionate about daily and continuous improvement.

Leave a Reply