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.
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
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.
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.