Hibernate is an Object-Relational Mapping framework in Java Programming Language. It maps the model of object-oriented to the relational database. It is an Open-source Software registered under the GNU Lesser General Public Licence 2.1.
This Hibernate tutorial will help you to understand Hibernate Framework easily. In this article, we will cover What is Hibernate framework, Why we use it, What are the ORM tool, History, Hibernate architecture, and its advantages.
What is Hibernate?
Hibernate is an Object-Relational Mapping framework in Java Programming Language. It maps the model of object-oriented to the relational database. It provides the framework which simplifies the interaction of Java Program with the database.
Hibernate is ORM(Object Relational Mapping) tool (which we will discuss further in this article) for Java Language. It can easily map our POJO class to the relational database of the system.
It is widely used by many Java developers across the country for better mapping of data in an application.
Why we use it?
As we know, when we have to connect our Java program with a database we use JDBC API for the connection.
- using JDBC we have to write some lines of code for mapping objects of the model class to the corresponding relational database. So, instead of writing these lines of code, in Hibernate developer don’t need to worry about it Hibernate itself takes care of mapping using XML files.
- JDBC supports SQL language only. Unlike this Hibernate provide a Hibernate Query Language which will make our code more efficient.
- In JDBC it’s the developer’s responsibility to handle the JDBC resultset. In Hibernate this manual work is handled by maintaining object table mapping.
- In JDBC, caching is maintained by manual coding. Unlike this, it is automatic.
What is the Hibernate ORM tool?
ORM acronyms to Object Relational Mapping tool that enables the developer to write easy code. It provides data persistence in the relational database. ORM tool helps you to map Java classes to database tables and map Java Data type to SQL type.
It consists of the following entities
- It provides an API to perform basic CRUD operation.
- It provides an API to specify queries.
- It provides a configuration for mapping metadata.
- It provides transactional objects.
History of Hibernate
- The development of Hibernate started in 2001 by Gavin King from Circus Technology. It was an alternative to EJB2. It was originally developed for the better persistence of data.
- Improvement in Hibernate leads to Hibernate2 in 2003.
- Later JBOSS Inc which is Red Hat now hired the Hibernate developers.
- In 2005, Hibernate version 3.0 was released with more key features.
- In 2011, Hibernate core 4.0.0 released which has new features such as an introduction to ServiceRegistry, session opening from session factory, etc.
- In 2012, Hibernate ORM 4.1.9 Final was released.
- In 2018, Hibernate ORM 5.4.0 Final was released which is the latest version.
Architecture of Hibernate
Hibernate is a framework that uses Objects to develop persistence logic which is independent of software. The architecture consists of a layered structure. It has the following layer
- Java Application Layer
- Hibernate Framework
- Backend API
- Database Schema
Following is the high level and detailed view of the architecture.
Let’s understand each Element from Hibernate Architecture first
- The configuration is a class which activates the Hibernate Framework.
- It consists of a configuration and properties file.
- It is present in org.hibernate.cfg package.
- The loading of the configuration happens at application initialization time.
- SessionFactory is created by the configuration object and then the session Object is instantiated.
- It is present in org.hibernate.SessionFactory package.
- For each database, we need one instance of SessionFactory.
- It is an immutable and also thread-safe object.
- SessionFactory create a Session object.
- It provides the actual connection with the database.
- Objects that are saved in the database are retrieved using the session objects.
Configuration config=new Configuration().configure(); //create a configuration object SessionFactory sf = config.buildSessionFactory(); //Get the SessionFactory Session session = sf.openSession(); //open a new session object session.close(); // close the session
- Transaction in computer science term means either all the operation is done or none of them.
- Transaction object is present in org.hibernate.Transaction package.
- In Hibernate, it is Handled by the Transaction manager.
- The query object is to retrieve or create objects.
- It uses SQL or HQL.
- It is used to execute a query.
- It is an object for retrieving entities.
- It is generally used for functionality like “search” screens.
Advantages of Hibernate
- It solves the data mismatch problem with the help of the ORM tool.
- It is an open-source software freely available and lightweight.
- It is database-independent. It can be used to connect with any database.
- It supports the HQL which is more powerful than SQL and is fully object-oriented.
- It is highly scalable.
- It has lazy loading that means it only fetches the necessary object that is required.
- More importantly, It is easy to learn for any developer.
Thus, We have seen the introductory part of the Hibernate Framework.
In the next article of this tutorial, we will cover the Environment Set-Up of the Hibernate Framework with a simple example.