Hibernate Architecture
Hibernate Architecture
Hibernate Architecture
software. In JDBC to develop persistence logic we deal with primitive types. Whereas Hibernate framework
we use Objects to develop persistence logic which are independent of database software. Hibernate
Architecture:
Hibernate ArchitectureConfiguration:
cfg.configure();
If the config file is not valid then it will throw an exception. If it is valid then it creates a meta-data in
memory and returns the meta-data to object to represent the config file.
SessionFactory:
SessionFactory is an Interface which is present in org.hibernate package and it is used to create Session
Object.
From cfg object it takes the JDBC information and create a JDBC Connection.
SessionFactory factory=cfg.buildSessionFactory();
Session:
Session is an interface which is present in org.hibernate package. Session object is created based upon
SessionFactory object i.e. factory.
openSession() is a method provided by the SessionFactory that creates and returns a new Session instance.
This session is not bound to any transaction or context and is independent of any ongoing transactions in
the application.
We can also use getCurrentSession, that returns a Session bound to the current context, which is usually
managed by a transaction manager or a framework like Spring.
Transaction:
Transaction object is used whenever we perform any operation and based upon that operation there is
some change in database.
Transaction object is used to give the instruction to the database to make the changes that happen because
of operation as a permanent by using commit() method.
Transaction tx=session.beginTransaction();
tx.commit();
Query:
This interface exposes some extra functionality beyond that provided by Session.iterate() and Session.find():
A particular page of the result set may be selected by calling setMaxResults(), setFirstResult().
Criteria:
The Session is a factory for Criteria. Criterion instances are usually obtained via the factory methods on
Restrictions.
Flow of working during operation in Hibernate Framework: Suppose We want to insert an Object to the
database. Here Object is nothing but persistence logic which we write on java program and create an object
of that program. If we want to insert that object in the database or we want to retrieve the object from the
database. Now the question is that how hibernate save the Object to the database or retrieve the object
from the database. There are several layers through which Hibernate framework go to achieve the above
task. Let us understand the layers/flow of Hibernate framework during performing operations:Flow of
working during operation in Hibernate FrameworkStage I: In first stage, we will write the persistence logic to
perform some specific operations to the database with the help of Hibernate Configuration file and
Hibernate mapping file. And after that we create an object of the particular class on which we wrote the
persistence logic. Stage II:In second stage, our class which contains the persistence logic will interact with
the hibernate framework where hibernate framework gives some abstraction do perform some task. Now
here the picture of java class is over. Now Hibernate is responsible to perform the persistence logic with the
help of layers which is below of Hibernate framework or we can say that the layers which are the internal
implementation of Hibernate. Stage III:In third stage, our hibernate framework interact which JDBC, JNDI,
JTA etc to go to the database to perform that persistence logic. Stage IV & V:In fourth & fifth stage,
hibernate is interact with Database with the help of JDBC driver. Now here hibernate perform that
persistence logic which is nothing but CRUD operation. If our persistence logic is to retrieve an record then
in the reverse order it will display on the console of our java program in terms of Object.