Introduction
Since
the very beginning of Hibernate framework it has been continuously
gaining support and popularity. I am therefore very much interested
in covering important aspects of Hibernate framework in my current as
well as upcoming posts on Java technologies.
Hibernate
was started in 2001 by Gavin King as an alternative to using
EJB2-style entity beans. It is important to know what persistence
actually is before starting our discussion on Hibernate. Persistence
is a process of storing the data to some permanent medium and
retrieving it back at any point of time even after the application
that had created the data ended. The purpose behind this framework
was to offer better persistence capabilities by simplifying the
complexities and allowing for missing features.
Hibernate is an object-relational
mapping (ORM) framework where ORM can be defined as,
Basic Architecture of Hibernate
It is important to note
here that the persistent objects synchronize data between application
and database layers.
Detailed Architecture of Hibernate
A Java class mapped to any
database table is called an Entity. The instance of an entity becomes
a persistent object with help of SessionFactoy which is a singleton
instance that implements Factory design pattern. SessionFactory loads
hibernate.cfg.xml file and with the help of TransactionFactory and
ConnectionProvider implements all the configuration settings on a
database.
The Session object is created out of
a SessionFactory object and it simply represents a single connection
with database. It is also noticeable that a Session can span through
multiple transactions where each transaction represents a single
atomic unit of work. Hibernate also provides built-in Transaction
APIs which abstracts away the application from underlying JDBC or JTA
transaction.
SessionFactory
- A factory for org.hibernate.Session instances.
- A thread-safe, immutable cache of compiled mappings for a single database.
- A client of org.hibernate.connection.ConnectionProvider.
- Optionally maintains a second level cache of data that is
reusable between transactions at a process or cluster level.
Session
- A single-threaded, short-lived object representing a conversation between the application and the persistent store.
- Wraps a JDBC
java.sql.Connection
. - Factory for org.hibernate.Transaction.
- Maintains a first level cache of the application’s
persistent objects and collections; this cache is used when
navigating the object graph or looking up objects by identifier.
Persistent/Attached objects
- Short-lived, single threaded objects containing persistent state and function.
- These can be ordinary JavaBeans/POJOs.
- They are associated with exactly one
Session
. - Once the Session is closed, they will be detached and free to
use in any application layer.
Transient/Detached objects
- Instances of entity classes that are not currently associated
with an active Session.
Transaction (org.hibernate.Transaction
)
- An optional, single-threaded, short-lived object used by the application to specify atomic units of work.
- Although the use of Transaction is optional yet the Transaction demarcation is never optional.
- It abstracts the application from the underlying JDBC, JTA or CORBA transaction.
- A Session might span through several transactions.
ConnectionProvider
(org.hibernate.connection.ConnectionProvider
)
- It provides pool of JDBC connections.
- It abstracts the application from underlying
javax.sql.DataSource or java.sql.DriverManager.
TransactionFactory
(org.hibernate.TransactionFactory
)
- A factory for org.hibernate.Transaction instances.