
Hibernate is an Object-Relational Mapping (ORM) framework that bridges the gap between object-oriented Java applications and relational databases. Let's explore its complete architecture:
1. Core Components of Hibernate Architecture
1.1. SessionFactory
- The central piece of Hibernate architecture as seen in your
HibernateUtils
class
- A thread-safe, immutable cache of compiled mappings for a single database
- Creates Session instances
- Holds second-level caches, connection pools, and dialect settings
- Usually created once at application startup and reused
1.2. Session
- A single-threaded, short-lived object representing a conversation between the application and the database
- Main runtime interface used by applications to perform CRUD operations
- Provides first-level cache (also called persistence context)
- Not thread-safe, should be used and then discarded
- In your application:
Session session = getFactory().getCurrentSession();
1.3. Transaction
-
Represents a unit of work with the database
-
Abstracts the underlying transaction mechanism (JDBC, JTA)
-
Used in your code as:
Transaction tx = session.beginTransaction();
try {
// operations
tx.commit();
} catch (Exception e) {
tx.rollback();
}
1.4. Configuration