Hibernate is a middleware used for object-relational mapping and for performing efficient object persistence.
To understand about the architecture of Hibernate,
let's first take a look at how Hibernate fits in the development of an application, between an application
and the database server.
A Java application code comprises of all the classes that define the business logic of application.
These classes communicate with the Hibernate.
Next comes, the Hibernate and its core interfaces, using which we could persist(store and retrieve) the objects of our business layer classes
by communicating with the database server.
Hibernate uses the Java core API, Java Database Connectivity(JDBC), Java Transaction API(JTA), Java Naming and Directory
Interface(JNDI) to communicate with database in
order to persist the state of object by performing create, read, update, delete(CRUD) operations.
For the reference, please look at the diagram below :
The architecture of Hibernate comprises of a few important interfaces. Let's discuss these interfaces and what exactly they are used for.
Configuration
The Configuration object is used to configure the Hibernate. This is the first object we use when using Hibernate. This object is used to specify the location of
mapping documents and some properties of Hibernate. Using Configuration object we can create a SessionFactory object, which is eventually used to create a Session
object to perform the object persistence operations.
The Hibernate interfaces that are used by the applications to perform the create, read, update, delete(CRUD) operations and querying the database are Session,
Transaction and Query.
Session
Session interface is the main Hibernate interface used by the applications. Session object is lightweight and can be easily created or destroyed without overburdening
the system resources. Session object is not thread-safe, hence it should not be used by multiple threads at one time. Session interface is mainly used for performing
persistence operations like storing and retrieving the objects in and out of the database.
Advertisement
SessionFactory
To obtain the Session object, we have to use the SessionFactory interface.
SessionFactory object is heavyweight and performance-wise, there should be only one object
of SessionFactory used by the whole application because it takes a lot of resources to create. A SessionFactory object can be shared by the multiple threads.
Transaction
Transaction interface allow you to make all the operations on the persisted object within a single transaction. During the execution of this single transaction, any
error between the start and the end of transaction leads to the cancellation of whole transaction and giving back the persisted object their original state(before the
beginning of transaction), thus helping maintaining the integrity of database.
Query
Query interface allows to query the database and also allows you to control how these queries are going to be executed. In Hibernate, queries are either written using
Hibernate Query Language(HQL) or SQL. Query interface binds the parameters sent within a query, controls the number of results returned by the execution of query and execute the query.
Callback Interfaces
Hibernate interfaces that allow an application to respond to the events performed by Hibernate, events such as saving, retrieving, updating, deleting the object. These
interfaces are Inteceptor, Lifecycle and Validatable and together they are also known as Callback Interfaces.