Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Hibernate Architecture

hibernate_Architecture

In the previous example we have seen, there is an 'Employee' class which is exactly same as a table named 'EMPLOYEE.'

Now, let's assume we have created a java application that creates an 'Employee' object and populates it.

Now, let us see the above diagram. And as we can see, there is something called Persistent Object(You can consider the 'Employee' object). This Persistent Object is the only object, we create in java. Then, this Persistent Object(Say 'Employee') is passed to the Hibernate Framework using some interfaces.

Then Hibernate loads this object to the database. So, it is the Persistent Object(Say 'Employee') which resides in our Java Application and the Hibernate Framework.



How does Hibernate link the fields of the java Object with the fields of the table?

i.e. Say there is a java class Employee :


class Employee{
  int id;
  String name;
  ---Getters & Setters---
}


Similarly in the database there is a table EMPLOYEE :


CREATE TABLE EMPLOYEE (ID INTEGER, NAME VARCHAR);

hibernate_Object_Relational_Mapping_ORM

In order to link these two, Hibernate uses a mapping file, where you can specify which field of the java class should be linked with what field in the table. Hibernate takes the help of this Mapping file to link the object and the table.


How does Hibernate save the Persistent Object to the database?

Usually Hibernate uses JDBC to connect to the database. So, in order to know which JDBC driver to use, Hibernate takes the help of a configuration file. We can specify the details about the JDBC driver, the DB(i.e. Oracle or DB2 or PostGres) in the configuration file and Hibernate will pick the right driver and DB based on it.


Core of Hibernate

Configuration

On top of the Hierarchy, there is the Configuration class which manages the configuration file and initializes the necessary classes needed to run Hibernate. So, whenever Hibernate starts, the configuration file is initialized.


hibernate_Configuration

Session Factory

Next comes the SessionFactory class and as the name suggests, it is a factory of Sessions.

i.e. Whenever you ask for a Session object, the SessionFactory is going to deliver a Session object for you.


hibernate_session_factory

Session

A Session object is used to deal with a particular transaction.

i.e. Say there is an Employee object and a Student object. Both needs to be saved to the same database. Now, you need to make sure that different sessions should be created for Employee and Student object so that their data don't collide with each other. It's same as the tigers and the lions are put in separate cages so that they don't fight each other.