Notes – Hibernate Architecture

Hibernate architecture is designed to manage all database-related operations in a structured and efficient way.

It works as a bridge between Java objects and relational database tables.


Core Layers of Hibernate Architecture

Hibernate has four key layers:


LayerDescription
Java ApplicationYour main Java code that uses Hibernate for DB operations.
Hibernate FrameworkCore Hibernate APIs like Session, Transaction, Query, etc.
JDBC/Database LayerLow-level JDBC API and actual database system.
Configuration LayerContains mapping and setup details (hibernate.cfg.xml, annotations).

Hibernate Architecture Components


ComponentRole in Architecture
ConfigurationReads configuration files and sets up Hibernate.
SessionFactoryCreates sessions. Heavy objectโ€”should be created only once.
SessionLightweight, short-lived object to interact with DB.
TransactionManages a unit of work like commit or rollback.
Query / HQLExecutes queries on Java objects using HQL or native SQL.
PersistenceRefers to storing and retrieving Java objects in/from the database.

Hibernate Workflow (Simple Flow)

  1. Application loads configuration.
  2. SessionFactory is created once at startup.
  3. Session is opened when DB interaction is needed.
  4. Transaction is started to ensure atomic operation.
  5. SQL or HQL queries are executed via Session.
  6. Transaction is committed or rolled back.
  7. Session is closed.

Diagram (Text Representation)

Java Application
โ†“
Configuration โ†’ SessionFactory
โ†“
Session
โ†“
Transaction
โ†“
JDBC / Database