Hibernate.orgCommunity Documentation
The diagram below provides a high-level view of the Hibernate architecture:
We do not have the scope in this document to provide a more detailed view of all the runtime architectures available; Hibernate is flexible and supports several different approaches. We will, however, show the two extremes: "minimal" architecture and "comprehensive" architecture.
This next diagram illustrates how Hibernate utilizes database and configuration data to provide persistence services, and persistent objects, to the application.
The "minimal" 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 "comprehensive" architecture abstracts the application away from the underlying JDBC/JTA APIs and allows Hibernate to manage the details.
Here are some definitions of the objects depicted in the diagrams:
org.hibernate.SessionFactory
)A threadsafe, immutable cache of compiled mappings for a single database. A factory for Session
and a client of ConnectionProvider
, SessionFactory
can hold an optional (second-level) cache of data that is reusable between transactions at a process, or cluster, level.
org.hibernate.Session
)A single-threaded, short-lived object representing a conversation between the application and the persistent store. It wraps a JDBC connection and is a factory for Transaction
. Session
holds a mandatory first-level cache of persistent objects that are used when navigating the object graph or looking up objects by identifier.
Short-lived, single threaded objects containing persistent state and business 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 (for example, directly as data transfer objects to and from presentation).
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
.
org.hibernate.Transaction
)(Optional) A single-threaded, short-lived object used by the application to specify atomic units of work. It abstracts the application from the underlying JDBC, JTA or CORBA transaction. A Session
might span several Transaction
s in some cases. However, transaction demarcation, either using the underlying API or Transaction
, is never optional.
org.hibernate.connection.ConnectionProvider
)(Optional) A factory for, and pool of, JDBC connections. It abstracts the application from underlying Datasource
or DriverManager
. It is not exposed to application, but it can be extended and/or implemented by the developer.
org.hibernate.TransactionFactory
)(Optional) A factory for Transaction
instances. It is not exposed to the application, but it can be extended and/or implemented by the developer.
Hibernate offers a range of optional extension interfaces you can implement to customize the behavior of your persistence layer. See the API documentation for details.
Given a "minimal" architecture, the application bypasses the Transaction
/TransactionFactory
and/or ConnectionProvider
APIs to communicate with JTA or JDBC directly.
An instance of a persistent class can be in one of three different states. These states are defined in relation to a persistence context. The Hibernate Session
object is the persistence context. The three different states are as follows:
The instance is not associated with any persistence context. It has no persistent identity or primary key value.
The instance is currently associated with a persistence context. It has a persistent identity (primary key value) and can have a corresponding row in the database. For a particular persistence context, Hibernate guarantees that persistent identity is equivalent to Java identity in relation to the in-memory location of the object.
The instance 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 can have a corresponding row in the database. For detached instances, Hibernate does not guarantee the relationship between persistent identity and Java identity.
JMX is the J2EE standard for the management of Java components. Hibernate can be managed via a JMX standard service. AN MBean implementation is provided in the distribution: org.hibernate.jmx.HibernateService
.
For an example of how to deploy Hibernate as a JMX service on the JBoss Application Server, please see the JBoss User Guide. JBoss AS also provides these benefits if you deploy using JMX:
Session Management: the Hibernate Session
's life cycle can be automatically bound to the scope of a JTA transaction. This means that you no longer have to manually open and close the Session
; this becomes the job of a JBoss EJB interceptor. You also do not have to worry about transaction demarcation in your code (if you would like to write a portable persistence layer use the optional Hibernate Transaction
API for this). You call the HibernateContext
to access a Session
.
HAR deployment: the Hibernate JMX service is deployed using a JBoss service deployment descriptor in an EAR and/or SAR file, as it supports all the usual configuration options of a Hibernate SessionFactory
. However, you still need to name all your mapping files in the deployment descriptor. If you use the optional HAR deployment, JBoss will automatically detect all mapping files in your HAR file.
Para más información sobre estas opciones, consulta la Guía de Usuario del JBoss AS.
Another feature available as a JMX service is runtime Hibernate statistics. See Sección 3.4.6, “Hibernate statistics” for more information.
Hibernate can also be configured as a JCA connector. Please see the website for more information. Please note, however, that at this stage Hibernate JCA support is under development.
Most applications using Hibernate need some form of "contextual" session, where a given session is in effect throughout the scope of a given context. However, across applications the definition of what constitutes a context is typically different; different contexts define different scopes to the notion of current. Applications using Hibernate prior to version 3.0 tended to utilize either home-grown ThreadLocal
-based contextual sessions, helper classes such as HibernateUtil
, or utilized third-party frameworks, such as Spring or Pico, which provided proxy/interception-based contextual sessions.
Starting with version 3.0.1, Hibernate added the SessionFactory.getCurrentSession()
method. Initially, this assumed usage of JTA
transactions, where the JTA
transaction defined both the scope and context of a current session. Given the maturity of the numerous stand-alone JTA TransactionManager
implementations, most, if not all, applications should be using JTA
transaction management, whether or not they are deployed into a J2EE
container. Based on that, the JTA
-based contextual sessions are all you need to use.
However, as of version 3.1, the processing behind SessionFactory.getCurrentSession()
is now pluggable. To that end, a new extension interface, org.hibernate.context.CurrentSessionContext
, and a new configuration parameter, hibernate.current_session_context_class
, have been added to allow pluggability of the scope and context of defining current sessions.
See the Javadocs for the org.hibernate.context.CurrentSessionContext
interface for a detailed discussion of its contract. It defines a single method, currentSession()
, by which the implementation is responsible for tracking the current contextual session. Out-of-the-box, Hibernate comes with three implementations of this interface:
org.hibernate.context.JTASessionContext
: current sessions are tracked and scoped by a JTA
transaction. The processing here is exactly the same as in the older JTA-only approach. See the Javadocs for details.
org.hibernate.context.ThreadLocalSessionContext
:current sessions are tracked by thread of execution. See the Javadocs for details.
org.hibernate.context.ManagedSessionContext
: current sessions are tracked by thread of execution. However, you are responsible to bind and unbind a Session
instance with static methods on this class: it does not open, flush, or close a Session
.
The first two implementations provide a "one session - one database transaction" programming model. This is also also known and used as session-per-request. The beginning and end of a Hibernate session is defined by the duration of a database transaction. If you use programmatic transaction demarcation in plain JSE without JTA, you are advised to use the Hibernate Transaction
API to hide the underlying transaction system from your code. If you use JTA, you can utilize the JTA interfaces to demarcate transactions. If you execute in an EJB container that supports CMT, transaction boundaries are defined declaratively and you do not need any transaction or session demarcation operations in your code. Refer to Capítulo 11, Transactions and Concurrency for more information and code examples.
The hibernate.current_session_context_class
configuration parameter defines which org.hibernate.context.CurrentSessionContext
implementation should be used. For backwards compatibility, if this configuration parameter is not set but a org.hibernate.transaction.TransactionManagerLookup
is configured, Hibernate will use the org.hibernate.context.JTASessionContext
. Typically, the value of this parameter would just name the implementation class to use. For the three out-of-the-box implementations, however, there are three corresponding short names: "jta", "thread", and "managed".
Copyright © 2004 Red Hat Middleware, LLC.