|
Hibernate example source code file (architecture.xml)
This example Hibernate source code file (architecture.xml) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Hibernate architecture.xml source code
<?xml version='1.0' encoding="UTF-8"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as
~ indicated by the @author tags or express copyright attribution
~ statements applied by the authors. All third-party contributions are
~ distributed under license by Red Hat Inc.
~
~ This copyrighted material is made available to anyone wishing to use, modify,
~ copy, or redistribute it subject to the terms and conditions of the GNU
~ Lesser General Public License, as published by the Free Software Foundation.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
~ for more details.
~
~ You should have received a copy of the GNU Lesser General Public License
~ along with this distribution; if not, write to:
~ Free Software Foundation, Inc.
~ 51 Franklin Street, Fifth Floor
~ Boston, MA 02110-1301 USA
-->
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
<!ENTITY % BOOK_ENTITIES SYSTEM "../HIBERNATE_-_Relational_Persistence_for_Idiomatic_Java.ent">
%BOOK_ENTITIES;
]>
<chapter id="architecture">
<title>Architecture
<section id="architecture-overview">
<title>Overview
<para>
The diagram below provides a high-level view of the Hibernate architecture:
</para>
<mediaobject>
<imageobject role="fo">
<imagedata fileref="images/overview.svg" format="SVG" align="center"/>
</imageobject>
<imageobject role="html">
<imagedata fileref="images/overview.png" format="PNG" align="center"/>
</imageobject>
</mediaobject>
<para>
Unfortunately we cannot provide a detailed view of all possible runtime architectures. Hibernate is
sufficiently flexible to be used in a number of ways in many, many architectures. We will, however,
illustrate 2 specifically since they are extremes.
</para>
<section id="architecture-overview-minimal">
<title>Minimal architecture
<para>
The "minimal" architecture has the application manage its own JDBC connections and provide those
connections to Hibernate; additionally the application manages transactions for itself. This approach
uses a minimal subset of Hibernate APIs.
</para>
<mediaobject>
<imageobject role="fo">
<imagedata fileref="images/lite.svg" format="SVG" align="center"/>
</imageobject>
<imageobject role="html">
<imagedata fileref="images/lite.png" format="PNG" align="center"/>
</imageobject>
</mediaobject>
</section>
<section id="architecture-overview-comprehensive">
<title>Comprehensive architecture
<para>
The "comprehensive" architecture abstracts the application away from the underlying JDBC/JTA APIs and
allows Hibernate to manage the details.
</para>
<mediaobject>
<imageobject role="fo">
<imagedata fileref="images/full_cream.svg" format="SVG" align="center"/>
</imageobject>
<imageobject role="html">
<imagedata fileref="images/full_cream.png" format="PNG" align="center"/>
</imageobject>
</mediaobject>
</section>
<section id="architecture-overview-terms">
<title>Basic APIs
<para>
Here are quick discussions about some of the API objects depicted in the preceding diagrams (you will
see them again in more detail in later chapters).
<variablelist spacing="compact">
<varlistentry>
<term>SessionFactory (org.hibernate.SessionFactory)
<listitem>
<para>
A thread-safe, immutable cache of compiled mappings for a single database.
A factory for <interfacename>org.hibernate.Session instances. A client
of <interfacename>org.hibernate.connection.ConnectionProvider. Optionally
maintains a <literal>second level cache of data that is reusable between
transactions at a process or cluster level.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Session (org.hibernate.Session)
<listitem>
<para>
A single-threaded, short-lived object representing a conversation between
the application and the persistent store. Wraps a JDBC
<interfacename>java.sql.Connection. Factory for
<interfacename>org.hibernate.Transaction. Maintains a
<literal>first level cache of persistent the application's persistent objects
and collections; this cache is used when navigating the object graph or looking up
objects by identifier.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Persistent objects and collections
<listitem>
<para>
Short-lived, single threaded objects containing persistent state and business
function. These can be ordinary JavaBeans/POJOs. They are associated with exactly one
<interfacename>org.hibernate.Session. Once the
<interfacename>org.hibernate.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). <xref linkend="objectstate"/> discusses transient,
persistent and detached object states.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Transient and detached objects and collections
<listitem>
<para>
Instances of persistent classes that are not currently associated with a
<interfacename>org.hibernate.Session. They may have been instantiated by
the application and not yet persisted, or they may have been instantiated by a
closed <interfacename>org.hibernate.Session.
<xref linkend="objectstate"/> discusses transient, persistent and detached object states.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Transaction (org.hibernate.Transaction)
<listitem>
<para>
(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 <interfacename>org.hibernate.Session might span several
<interfacename>org.hibernate.Transactions in some cases. However,
transaction demarcation, either using the underlying API or
<interfacename>org.hibernate.Transaction, is never optional.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>ConnectionProvider (org.hibernate.connection.ConnectionProvider)
<listitem>
<para>
(Optional) A factory for, and pool of, JDBC connections. It abstracts the application from
underlying <interfacename>javax.sql.DataSource or
<interfacename>java.sql.DriverManager. It is not exposed to application,
but it can be extended and/or implemented by the developer.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>TransactionFactory (org.hibernate.TransactionFactory)
<listitem>
<para>
(Optional) A factory for <interfacename>org.hibernate.Transaction
instances. It is not exposed to the application, but it can be extended and/or
implemented by the developer.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Extension Interfaces
<listitem>
<para>
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.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</section>
</section>
<section id="architecture-jmx" revision="1">
<title>JMX Integration
<para>
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:
<literal>org.hibernate.jmx.HibernateService.
</para>
<para>
Another feature available as a JMX service is runtime Hibernate statistics. See
<xref linkend="configuration-optional-statistics"/> for more information.
</para>
</section>
<section id="architecture-current-session" revision="2">
<title>Contextual sessions
<para>
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 <literal>ThreadLocal-based
contextual sessions, helper classes such as <literal>HibernateUtil, or utilized
third-party frameworks, such as Spring or Pico, which provided proxy/interception-based contextual sessions.
</para>
<para>
Starting with version 3.0.1, Hibernate added the <literal>SessionFactory.getCurrentSession()
method. Initially, this assumed usage of <literal>JTA transactions, where the
<literal>JTA transaction defined both the scope and context of a current session.
Given the maturity of the numerous stand-alone
<literal>JTA TransactionManager implementations, most, if not all,
applications should be using <literal>JTA transaction management, whether or not
they are deployed into a <literal>J2EE container. Based on that, the
<literal>JTA-based contextual sessions are all you need to use.
</para>
<para>
However, as of version 3.1, the processing behind
<literal>SessionFactory.getCurrentSession() is now pluggable. To that
end, a new extension interface, <literal>org.hibernate.context.spi.CurrentSessionContext,
and a new configuration parameter, <literal>hibernate.current_session_context_class,
have been added to allow pluggability of the scope and context of defining current sessions.
</para>
<para>
See the Javadocs for the <literal>org.hibernate.context.spi.CurrentSessionContext
interface for a detailed discussion of its contract. It defines a single method,
<literal>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:
</para>
<itemizedlist>
<listitem>
<para>
<literal>org.hibernate.context.internal.JTASessionContext: current sessions
are tracked and scoped by a <literal>JTA transaction. The processing
here is exactly the same as in the older JTA-only approach. See the Javadocs
for details.
</para>
</listitem>
<listitem>
<para>
<literal>org.hibernate.context.internal.ThreadLocalSessionContext:current
sessions are tracked by thread of execution. See the Javadocs for details.
</para>
</listitem>
<listitem>
<para>
<literal>org.hibernate.context.internal.ManagedSessionContext: current
sessions are tracked by thread of execution. However, you are responsible to
bind and unbind a <literal>Session instance with static methods
on this class: it does not open, flush, or close a <literal>Session.
</para>
</listitem>
</itemizedlist>
<para>
The first two implementations provide a "one session - one database transaction" programming
model. This is also known and used as <emphasis>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 <literal>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 <xref linkend="transactions"/> for more information and code examples.
</para>
<para>
The <literal>hibernate.current_session_context_class configuration parameter
defines which <literal>org.hibernate.context.spi.CurrentSessionContext implementation
should be used. For backwards compatibility, if this configuration parameter is not set
but a <literal>org.hibernate.transaction.TransactionManagerLookup is configured,
Hibernate will use the <literal>org.hibernate.context.internal.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".
</para>
</section>
</chapter>
Other Hibernate examples (source code examples)
Here is a short list of links related to this Hibernate architecture.xml source code file:
|