Architecture
Overview
A (very) high-level view of the Hibernate architecture:
This diagram shows Hibernate using the database and configuration data to
provide persistence services (and persistent objects) to the application.
We would like to show a more detailed view of the runtime architecture.
Unfortunately, Hibernate is flexible and supports several approaches. We will
show the two extremes. The "lite" architecture has the application
provide its own JDBC connections and manage its own transactions. This approach
uses a minimal subset of Hibernate's APIs:
The "full cream" architecture abstracts the application away from the
underlying JDBC/JTA APIs and lets Hibernate take care of the details.
Heres some definitions of the objects in the diagrams:
SessionFactory (org.hibernate.SessionFactory)
A threadsafe (immutable) cache of compiled mappings for a single database.
A factory for Session and a client of
ConnectionProvider. Might hold an optional (second-level)
cache of data that is reusable between transactions, at a
process- or cluster-level.
Session (org.hibernate.Session)
A single-threaded, short-lived object representing a conversation between
the application and the persistent store. Wraps a JDBC connection. Factory
for Transaction. Holds a mandatory (first-level) cache
of persistent objects, used when navigating the object graph or looking up
objects by identifier.
Persistent objects and collections
Short-lived, single threaded objects containing persistent state and business
function. These might be ordinary JavaBeans/POJOs, the only special thing about
them is that they are currently associated with (exactly one)
Session. As soon as the Session is closed,
they will be detached and free to use in any application layer (e.g. directly
as data transfer objects to and from presentation).
Transient and detached objects and collections
Instances of persistent classes that are not currently associated with a
Session. They may have been instantiated by
the application and not (yet) persisted or they may have been instantiated by a
closed Session.
Transaction (org.hibernate.Transaction)
(Optional) A single-threaded, short-lived object used by the application to
specify atomic units of work. Abstracts application from underlying JDBC,
JTA or CORBA transaction. A Session might span several
Transactions in some cases. However, transaction demarcation,
either using the underlying API or Transaction, is never
optional!
ConnectionProvider (org.hibernate.connection.ConnectionProvider)
(Optional) A factory for (and pool of) JDBC connections. Abstracts application from
underlying Datasource or DriverManager.
Not exposed to application, but can be extended/implemented by the developer.
TransactionFactory (org.hibernate.TransactionFactory)
(Optional) A factory for Transaction instances. Not exposed to the
application, but can be extended/implemented by the developer.
Extension Interfaces
Hibernate offers many optional extension interfaces you can implement to customize
the behavior of your persistence layer. See the API documentation for details.
Given a "lite" architecture, the application bypasses the
Transaction/TransactionFactory and/or
ConnectionProvider APIs to talk to JTA or JDBC directly.
Instance states
An instance of a persistent classes may be in one of three different states,
which are defined with respect to a persistence context.
The Hibernate Session object is the persistence context:
transient
The instance is not, and has never been associated with
any persistence context. It has no persistent identity
(primary key value).
persistent
The instance is currently associated with a persistence
context. It has a persistent identity (primary key value)
and, perhaps, a corresponding row in the database. For a
particular persistence context, Hibernate
guarantees that persistent identity
is equivalent to Java identity.
detached
The instance is was once associated with a persistence
context, but that context was closed, or the instance
was serialized to another process. It has a persistent
identity and, perhaps, a corrsponding row in the database.
For detached instances, Hibernate makes no guarantees
about the relationship between persistent identity and
Java identity.
JMX Integration
JMX is the J2EE standard for management of Java components. Hibernate may be managed via
a JMX standard MBean but because most application servers do not yet support JMX, Hibernate
also affords some non-standard configuration mechanisms.
Please see the Hibernate website for more information on how to configure Hibernate to
run as a JMX component inside JBoss. Integration with other JMX containers is also
possible.
TODO: More documentation about JMX integration.
JCA Support
Hibernate may also be configured as a JCA connector. Please see the website for more
details.
TODO: More documentation about JCA integration.