Hibernate Tutorials : Hibernate Architecture

hibernate architecture

Hibernate Architecture

Now we know what is Hibernate and ORM all about, but it is important to understand the internal architecture of every framework. Which helps us to understand the internal working of the framework. Hibernate is widely used framework across the world. Because it maps the Objects to relational database and visa versa. Hibernate API is abstracted through it’s different layers, means user is not aware what’s happening internally. Following is the high level architecture of hibernate.  

hibernate architecture  

How things working in Hibernate

  • The core of the Hibernate API is it’s configuration files. Hibernate uses configuration files to create a configuration object during the application initialization.
  • Then Hibernate creates SessionFactory with the help of configuration object. SessionFactory is thread safe and can be used by different threads of the application.
  • SessionFactory is now ready to create Session object which creates the physical connection between application and database to communicate with each other. Session object is not thread safe and should be handled carefully in application.
  • Session object can now create Transaction object to perform operation in application. Transaction is basically a operation like query database, update records etc.
  • After that we can close the particular connection.

Let’s understand different terms


SessionFactory

A factory for the Session objects. Optionally maintains second level cache which can be reused by different transactions. SessionFactory is thread safe and can be accessed by different threads in application.

org.hibernate.SessionFactory
 
Session

A Session is single threaded and short lived object created by SessionFactory. It maintains the first level cache. Session object is responsible for the communication between Application and Database. Session object is not thread safe.

org.hibernate.Session
 

Persistent Object

You can think of Persistent objects as data transfer objects. Usually associated with exactly one Session object. Once session is closed, they are free. Persistent objects are short lived and single threaded.  

Transient / Detached Object

You can think of Transient objects as instance of persistent classes that are not associated with any Session object. They have been initialized but not persisted yet.  

Transaction

Transaction object is short lived and single threaded which is used to perform atomic units of work in application.
  

ConnectionProvider

Factory for the JDBC connections. It abstracts the application from javax.sql.DataSource or java.sql.DriverManager.  

TransactionFactory

Factory for the Transaction instances which is not exposed to the application.  

Leave a Reply

Your email address will not be published. Required fields are marked *