Hibernate is a framework which is used to develop persistence logic which is independent of Database 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.
- Provides database-independent persistence by mapping Java objects to database tables.
- Uses layers like SessionFactory, Session, and JDBC to manage CRUD operations efficiently.
Hibernate Internal Architecture
Hibernate Architecture Shows how Java objects interact with the database through Hibernate layers and APIs.

Configuration
- Configuration is a class which is present in org.hibernate.cfg package. It activates Hibernate framework. It reads both configuration file and mapping files.
- It checks whether the config file is syntactically correct or not.
- 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.
It activate Hibernate Framework Configuration cfg = new Configuration();
it read both cfg file and mapping files cfg.configure();
SessionFactory
- SessionFactory is an Interface which is present in org.hibernate package and it is used to create Session Object.
- It is immutable and thread-safe in nature.
buildSessionFactory() method gathers the meta-data which is in the cfg Object.
SessionFactory uses the configuration metadata to manage database connections through connection providers and pooling mechanisms.
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.
- It opens the Connection/Session with Database software through Hibernate Framework.
- It is a light-weight object and it is not thread-safe.
- Session object is used to perform CRUD operations.
Session session = factory.openSession();
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.
getCurrentSession: it returns a Session bound to the current context, which is usually managed by a transaction manager or a framework like Spring. It requires configuration like hibernate.current_session_context_class (e.g., thread or Spring-managed context).
Session session = sessionFactory.getCurrentSession();
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
- Query is an interface in the org.hibernate package used to execute HQL queries.
- It is created using the Session.createQuery() method to interact with the database.
- It supports advanced features like pagination (setMaxResults(), setFirstResult()) and named parameters for better query handling.
Criteria
- Criteria is a simplified API for retrieving entities by composing Criterion objects.
- 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
The below image showing how the hibernate application internally works:

Stage I: Transient State
- Create a Java object using new keyword.
- Not associated with Hibernate or database yet.
Stage II: Persistent State
- Object is associated with a Hibernate Session using save() or persist().
- Any changes are automatically tracked and synchronized with the database.
Stage III: Detached State
- Session is closed or object is evicted.
- Object exists in memory but is no longer tracked by Hibernate.
- Can be reattached using update() or merge().
Stage IV: Removed State
- Object is marked for deletion using delete().
- Removal happens when the transaction is committed to the database.
Stage V: Data Flow to Database
- Hibernate interacts with JDBC to execute the CRUD operation.
- For retrieval, database results are mapped back to Java objects.