The Hibernate architecture includes many persistent object, session factory, transaction factory, connection factory, session, transaction etc.
Hibernate is a layer between your application and database and provide the persistence for your application via set of API’s, performing transformation
Above is a high level diagram to understand different components of hibernate.
- Configuration (org.hibernate.cfg.Configuration)It allows the application on startup, to specify properties and mapping documents to be used when creating a SessionFactory. Properties file contains database connection setup info while mapping specifies the classes to be mapped.
- SessionFactory (org.hibernate.SessionFactory)
It’s a thread-safe immutable object created per database & mainly used for creating Sessions.It caches generated SQL statements and other mapping metadata that Hibernate uses at runtime.
- Session (org.hibernate.Session)
It’s a single-threaded object used to perform create, read, update and delete operations for instances of mapped entity classes. Since it’s not thread-safe, it should not be long-lived and each thread/transaction should obtain its own instance from a SessionFactory.
The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.
- Transaction (org.hibernate.Transaction)
It’s a single-thread object used by the application to define units of work. A transaction is associated with a Session. Transactions abstract application code from underlying transaction implementations(JTA/JDBC), allowing the application to control transaction boundaries via a consistent API. It’s an Optional API and application may choose not to use it.
- Query (org.hibernate.Query)
A single-thread object used to perform query on underlying database. A Session is a factory for Query. Both HQL(Hibernate Query Language) & SQL can be used with Query object.
Hibernate provides a lot of flexibility in use. It is called “Lite” architecture when we only uses the object relational mapping component. While in “Full Cream” architecture all the three component Object Relational mapping, Connection Management and Transaction Management) are used.