Update reference documentation generation tools to get source highlighting [SPRNET-1045]
This commit is contained in:
@@ -1,8 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<chapter id="orm">
|
||||
<!--
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
-->
|
||||
<chapter xml:id="orm" xmlns="http://docbook.org/ns/docbook" version="5">
|
||||
<title>Object Relational Mapping (ORM) data access</title>
|
||||
|
||||
<section id="orm-introduction">
|
||||
<section xml:id="orm-introduction">
|
||||
<title>Introduction</title>
|
||||
|
||||
<para>The Spring Framework provides integration with <emphasis>NHibernate
|
||||
@@ -31,8 +48,8 @@
|
||||
<listitem>
|
||||
<para><emphasis>Ease of testing.</emphasis> Spring's IoC approach
|
||||
makes it easy to swap the implementations and config locations of
|
||||
Hibernate <interfacename>SessionFactory</interfacename> instances,
|
||||
ADO.NET <interfacename>DbProvider</interfacename> instances,
|
||||
Hibernate <literal>SessionFactory</literal> instances,
|
||||
ADO.NET <literal>DbProvider</literal> instances,
|
||||
transaction managers, and mapper object implementations (if needed).
|
||||
This makes it much easier to isolate and test each piece of
|
||||
persistence-related code in isolation.</para>
|
||||
@@ -52,18 +69,18 @@
|
||||
<listitem>
|
||||
<para><emphasis>General resource management.</emphasis> Spring
|
||||
application contexts can handle the location and configuration of
|
||||
Hibernate <interfacename>ISessionFactory</interfacename> instances,
|
||||
ADO.NET <interfacename>DbProvider</interfacename> instances and other
|
||||
Hibernate <literal>ISessionFactory</literal> instances,
|
||||
ADO.NET <literal>DbProvider</literal> instances and other
|
||||
related resources. This makes these values easy to manage and change.
|
||||
Spring offers efficient, easy and safe handling of persistence
|
||||
resources. For example: related code using NHibernate generally needs
|
||||
to use the same NHibernate <interfacename>Session</interfacename> for
|
||||
to use the same NHibernate <literal>Session</literal> for
|
||||
efficiency and proper transaction handling. Spring makes it easy to
|
||||
transparently create and bind a <interfacename>Session</interfacename>
|
||||
transparently create and bind a <literal>Session</literal>
|
||||
to the current thread, either by using an explicit 'template' wrapper
|
||||
class at the code level or by exposing a current
|
||||
<interfacename>Session</interfacename> through the Hibernate
|
||||
<interfacename>SessionFactory</interfacename> (for DAOs based on plain
|
||||
<literal>Session</literal> through the Hibernate
|
||||
<literal>SessionFactory</literal> (for DAOs based on plain
|
||||
Hibernate 1.2 API). Thus Spring solves many of the issues that
|
||||
repeatedly arise from typical NHibernate usage, for any transaction
|
||||
environment (local or distributed).</para>
|
||||
@@ -110,7 +127,7 @@
|
||||
value proposition.</para>
|
||||
</section>
|
||||
|
||||
<section id="orm-hibernate">
|
||||
<section xml:id="orm-hibernate">
|
||||
<title>NHibernate</title>
|
||||
|
||||
<para>We will start with a coverage of <ulink
|
||||
@@ -127,7 +144,7 @@
|
||||
the 'template' approach. Spring supports both NHibernate 1.0 and
|
||||
NHibernate 1.2 via separate .dlls with the same internal namespace.</para>
|
||||
|
||||
<section id="orm-resource-mngmnt">
|
||||
<section xml:id="orm-resource-mngmnt">
|
||||
<title>Resource management</title>
|
||||
|
||||
<para>Typical business applications are often cluttered with repetitive
|
||||
@@ -140,18 +157,18 @@
|
||||
for appropriate conversion of specific API exceptions to a common
|
||||
infrastructure exception hierarchy. Spring introduces a DAO exception
|
||||
hierarchy, applicable to any data access strategy. For direct ADO.NET,
|
||||
the <classname>AdoTemplate</classname> class mentioned in a previous
|
||||
the <literal>AdoTemplate</literal> class mentioned in a previous
|
||||
section cares for connection handling, and for proper conversion of
|
||||
ADO.NET data access exceptions (not even singly rooted in .NET 1.1) to
|
||||
Spring's <classname>DataAccessException</classname> hierarchy, including
|
||||
Spring's <literal>DataAccessException</literal> hierarchy, including
|
||||
translation of database-specific SQL error codes to meaningful exception
|
||||
classes. It supports both distributed and local transactions, via
|
||||
respective Spring transaction managers.</para>
|
||||
|
||||
<para>Spring also offers Hibernate support, consisting of a
|
||||
<classname>HibernateTemplate</classname> analogous to
|
||||
<classname>AdoTemplate</classname>, a
|
||||
<classname>HibernateInterceptor</classname>, and a Hibernate transaction
|
||||
<literal>HibernateTemplate</literal> analogous to
|
||||
<literal>AdoTemplate</literal>, a
|
||||
<literal>HibernateInterceptor</literal>, and a Hibernate transaction
|
||||
manager. The major goal is to allow for clear application layering, with
|
||||
any data access and transaction technology, and for loose coupling of
|
||||
application objects. No more business service dependencies on the data
|
||||
@@ -169,7 +186,7 @@
|
||||
the business services),and so on.</para>
|
||||
</section>
|
||||
|
||||
<section id="orm-tx-mgmt">
|
||||
<section xml:id="orm-tx-mgmt">
|
||||
<title>Transaction Management</title>
|
||||
|
||||
<para>While NHibernate offers an API for transaction management you will
|
||||
@@ -183,8 +200,8 @@
|
||||
the other the .NET 2.0 TransactionScope API.</para>
|
||||
|
||||
<para>The first strategy is encapsulated in the class
|
||||
<classname>Spring.Data.NHibernate.HibernateTransactionManager
|
||||
</classname>in both the <literal>Spring.Data.NHibernate
|
||||
<literal>Spring.Data.NHibernate.HibernateTransactionManager
|
||||
</literal>in both the <literal>Spring.Data.NHibernate
|
||||
</literal>namespace. This strategy is preferred when you are using a
|
||||
single database. ADO.NET operations can also participate in the same
|
||||
transaction, either by using AdoTemplate or by retrieving the ADO.NET
|
||||
@@ -198,10 +215,10 @@
|
||||
configuration file to gain the benefits of easy configuration for a
|
||||
particular runtime environment and as the basis for the configuration of
|
||||
a data access layer also configured using XML. An XML fragment showing
|
||||
the declaration of <classname>HibernateTransactionManager</classname> is
|
||||
the declaration of <literal>HibernateTransactionManager</literal> is
|
||||
shown below.</para>
|
||||
|
||||
<programlisting> <object id="HibernateTransactionManager"
|
||||
<programlisting language="myxml"> <object id="HibernateTransactionManager"
|
||||
type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate">
|
||||
|
||||
<property name="DbProvider" ref="DbProvider"/>
|
||||
@@ -210,14 +227,14 @@
|
||||
</object></programlisting>
|
||||
|
||||
<para>The important property of
|
||||
<classname>HibernateTransactionManager</classname> are the references to
|
||||
<literal>HibernateTransactionManager</literal> are the references to
|
||||
the DbProvider and the Hibernate ISessionFactory. For more information
|
||||
on the DbProvider, refer to the chapter <link
|
||||
linkend="dbprovider">DbProvider</link> and the following section on
|
||||
SessionFactory set up.</para>
|
||||
|
||||
<para>The second strategy is to use the class
|
||||
<classname>Sping.Data.TxScopeTransactionManager</classname> that uses
|
||||
<literal>Sping.Data.TxScopeTransactionManager</literal> that uses
|
||||
.NET 2.0 System.Transaction namespace and its corresponding
|
||||
TransactionScope API. This is preferred when you are using multiple
|
||||
transactional resources, such as multiple databases.</para>
|
||||
@@ -226,7 +243,7 @@
|
||||
the transaction (scope in the general demarcation sense, not
|
||||
System.Transaction sense). If there is no transaction then a new Session
|
||||
will be opened for each operation. The exception to this rule is when
|
||||
using the <classname>OpenSessionInViewModule</classname> in a web
|
||||
using the <literal>OpenSessionInViewModule</literal> in a web
|
||||
application in single session mode (see <xref
|
||||
linkend="orm-hibernate-web" />). In this case the session will be
|
||||
created on the start of the web request and closed on the end of the
|
||||
@@ -240,22 +257,22 @@
|
||||
of the web request.</para>
|
||||
</section>
|
||||
|
||||
<section id="orm-session-factory-setup">
|
||||
<title><interfacename>SessionFactory</interfacename> set up in a Spring
|
||||
<section xml:id="orm-session-factory-setup">
|
||||
<title><literal>SessionFactory</literal> set up in a Spring
|
||||
container</title>
|
||||
|
||||
<para>To avoid tying application objects to hard-coded resource lookups,
|
||||
Spring allows you to define resources like a
|
||||
<interfacename>DbProvider</interfacename> or a Hibernate
|
||||
<interfacename>SessionFactory</interfacename> as objects in an
|
||||
<literal>DbProvider</literal> or a Hibernate
|
||||
<literal>SessionFactory</literal> as objects in an
|
||||
application context. Application objects that need to access resources
|
||||
just receive references to such pre-defined instances via object
|
||||
references (the DAO definition in the next section illustrates this).
|
||||
The following excerpt from an XML application context definition shows
|
||||
how to set up Spring's ADO.NET DbProvider and a Hibernate
|
||||
<interfacename>SessionFactory</interfacename> on top of it:</para>
|
||||
<literal>SessionFactory</literal> on top of it:</para>
|
||||
|
||||
<programlisting><objects xmlns="http://www.springframework.net"
|
||||
<programlisting language="myxml"><objects xmlns="http://www.springframework.net"
|
||||
xmlns:db="http://www.springframework.net/database">
|
||||
|
||||
|
||||
@@ -318,7 +335,7 @@
|
||||
array so multiple configuration files are supported.</para>
|
||||
|
||||
<para>There are other properties in
|
||||
<classname>LocalSessionFactoryObject</classname> that relate to the
|
||||
<literal>LocalSessionFactoryObject</literal> that relate to the
|
||||
integration of Spring with NHibernate. The property
|
||||
<literal>ExposeTransactionAwareSessionFactory</literal> is discussed
|
||||
below and allows you to use Spring's declarative transaction demarcation
|
||||
@@ -335,7 +352,7 @@
|
||||
</listitem>
|
||||
|
||||
<listitem>
|
||||
<para>Delegate to the <classname>DbProvider</classname> itself as
|
||||
<para>Delegate to the <literal>DbProvider</literal> itself as
|
||||
the NHibernate connection provider instead of listing it via
|
||||
property hibernate.connection.provider via
|
||||
<literal>HibernateProperties</literal>.</para>
|
||||
@@ -345,9 +362,9 @@
|
||||
<para>If you specify both the property hibernate.connection.provider and
|
||||
DbProvider (as shown above) the configuration of the property
|
||||
hibernate.connection.provider is used and a warning level message is
|
||||
logged. If you use Spring's <classname>DbProvider</classname> as the
|
||||
logged. If you use Spring's <literal>DbProvider</literal> as the
|
||||
NHibernate connection provider then you can take advantage of
|
||||
<classname>IDbProvider</classname> implementations that will let you
|
||||
<literal>IDbProvider</literal> implementations that will let you
|
||||
change the connection string at runtime such as <link lang=""
|
||||
linkend="dbprovider-usercredentials">UserCredentialsDbProvider</link>
|
||||
and <link
|
||||
@@ -360,39 +377,39 @@
|
||||
linkend="dbprovider-multidelegating">MultiDelegatingDbProvider</link>
|
||||
only change the connection string at runtime based on values in thread
|
||||
local storage and do not clear out the Hibernate cache that is unique
|
||||
to each <classname>ISessionFactory</classname> instance. As such, they
|
||||
to each <literal>ISessionFactory</literal> instance. As such, they
|
||||
are only useful for selecting at runtime a single database instance.
|
||||
Cleaning up an existing session factory when switching to a new
|
||||
database is left to user code. Creating a new session factory per
|
||||
connection string (assuming the same mapping files can be used across
|
||||
all databases connections) is not currently supported. To support this
|
||||
functionality, you can subclass
|
||||
<classname>LocalSessionFactoryObject</classname> and override the
|
||||
<literal>LocalSessionFactoryObject</literal> and override the
|
||||
method <literal>ISessionFactory NewSessionFactory(Configuration
|
||||
config)</literal> so that it returns an implementation of
|
||||
<classname>ISessionFactory</classname> that selects among multiple
|
||||
<literal>ISessionFactory</literal> that selects among multiple
|
||||
instances based on values in thread local storage, much like the
|
||||
implementation of
|
||||
<classname>MultiDelegatingDbProvider</classname>.</para>
|
||||
<literal>MultiDelegatingDbProvider</literal>.</para>
|
||||
</note>
|
||||
</section>
|
||||
|
||||
<section id="orm-hibernate-template">
|
||||
<title>The <classname>HibernateTemplate</classname></title>
|
||||
<section xml:id="orm-hibernate-template">
|
||||
<title>The <literal>HibernateTemplate</literal></title>
|
||||
|
||||
<para>The basic programming model for templating looks as follows for
|
||||
methods that can be part of any custom data access object or business
|
||||
service. There are no restrictions on the implementation of the
|
||||
surrounding object at all, it just needs to provide a Hibernate
|
||||
<interfacename>SessionFactory</interfacename>. It can get the latter
|
||||
<literal>SessionFactory</literal>. It can get the latter
|
||||
from anywhere, but preferably as an object reference from a Spring IoC
|
||||
container - via a simple <methodname>SessionFactory</methodname>
|
||||
property setter. The following snippets show a DAO definition in a
|
||||
Spring container, referencing the above defined
|
||||
<interfacename>SessionFactory</interfacename>, and an example for a DAO
|
||||
<literal>SessionFactory</literal>, and an example for a DAO
|
||||
method implementation.</para>
|
||||
|
||||
<programlisting><objects>
|
||||
<programlisting language="myxml"><objects>
|
||||
|
||||
<object id="CustomerDao" type="Spring.Northwind.Dao.NHibernate.HibernateCustomerDao, Spring.Northwind.Dao.NHibernate">
|
||||
<property name="SessionFactory" ref="MySessionFactory"/>
|
||||
@@ -402,7 +419,7 @@
|
||||
|
||||
<para></para>
|
||||
|
||||
<programlisting>public class HibernateCustomerDao : ICustomerDao {
|
||||
<programlisting language="csharp">public class HibernateCustomerDao : ICustomerDao {
|
||||
|
||||
private HibernateTemplate hibernateTemplate;
|
||||
|
||||
@@ -418,15 +435,15 @@
|
||||
}
|
||||
}</programlisting>
|
||||
|
||||
<para>The <classname>HibernateTemplate</classname> class provides many
|
||||
<para>The <literal>HibernateTemplate</literal> class provides many
|
||||
methods that mirror the methods exposed on the Hibernate
|
||||
<interfacename>Session</interfacename> interface, in addition to a
|
||||
<literal>Session</literal> interface, in addition to a
|
||||
number of convenience methods such as the one shown above. If you need
|
||||
access to the <interfacename>Session</interfacename> to invoke methods
|
||||
that are not exposed on the <classname>HibernateTemplate</classname>,
|
||||
access to the <literal>Session</literal> to invoke methods
|
||||
that are not exposed on the <literal>HibernateTemplate</literal>,
|
||||
you can always drop down to a callback-based approach like so.</para>
|
||||
|
||||
<programlisting>public class HibernateCustomerDao : ICustomerDao {
|
||||
<programlisting language="csharp">public class HibernateCustomerDao : ICustomerDao {
|
||||
|
||||
private HibernateTemplate hibernateTemplate;
|
||||
|
||||
@@ -454,7 +471,7 @@
|
||||
generics, you can avoid the typecast and write code like the
|
||||
following</para>
|
||||
|
||||
<programlisting>IList<Supplier> suppliers = HibernateTemplate.ExecuteFind<Supplier>(
|
||||
<programlisting language="csharp">IList<Supplier> suppliers = HibernateTemplate.ExecuteFind<Supplier>(
|
||||
delegate(ISession session)
|
||||
{
|
||||
return session.CreateQuery("from Supplier s were s.Code = ?")
|
||||
@@ -466,22 +483,22 @@
|
||||
inside the anonymous delegate implementation.</para>
|
||||
|
||||
<para>A callback implementation effectively can be used for any
|
||||
Hibernate data access. <classname>HibernateTemplate</classname> will
|
||||
ensure that <interfacename>Session</interfacename> instances are
|
||||
Hibernate data access. <literal>HibernateTemplate</literal> will
|
||||
ensure that <literal>Session</literal> instances are
|
||||
properly opened and closed, and automatically participate in
|
||||
transactions. The template instances are thread-safe and reusable, they
|
||||
can thus be kept as instance variables of the surrounding class. For
|
||||
simple single step actions like a single Find, Load, SaveOrUpdate, or
|
||||
Delete call, <classname>HibernateTemplate</classname> offers alternative
|
||||
Delete call, <literal>HibernateTemplate</literal> offers alternative
|
||||
convenience methods that can replace such one line callback
|
||||
implementations. Furthermore, Spring provides a convenient
|
||||
<classname>HibernateDaoSupport</classname> base class that provides a
|
||||
<literal>HibernateDaoSupport</literal> base class that provides a
|
||||
<methodname>SessionFactory</methodname> property for receiving a
|
||||
<interfacename>SessionFactory</interfacename> and for use by subclasses.
|
||||
<literal>SessionFactory</literal> and for use by subclasses.
|
||||
In combination, this allows for very simple DAO implementations for
|
||||
typical requirements:</para>
|
||||
|
||||
<programlisting>public class HibernateCustomerDao : HibernateDaoSupport, ICustomerDao
|
||||
<programlisting language="csharp">public class HibernateCustomerDao : HibernateDaoSupport, ICustomerDao
|
||||
{
|
||||
public Customer SaveOrUpdate(Customer customer)
|
||||
{
|
||||
@@ -491,28 +508,28 @@
|
||||
}</programlisting>
|
||||
</section>
|
||||
|
||||
<section id="orm-hibernate-daos">
|
||||
<section xml:id="orm-hibernate-daos">
|
||||
<title>Implementing Spring-based DAOs without callbacks</title>
|
||||
|
||||
<para>As an alternative to using Spring's
|
||||
<classname>HibernateTemplate</classname> to implement DAOs, data access
|
||||
<literal>HibernateTemplate</literal> to implement DAOs, data access
|
||||
code can also be written in a more traditional fashion, without wrapping
|
||||
the Hibernate access code in a callback, while still respecting and
|
||||
participating in Spring's generic
|
||||
<classname>DataAccessException</classname> hierarchy. The
|
||||
<classname>HibernateDaoSupport</classname> base class offers methods to
|
||||
access the current transactional <interfacename>Session</interfacename>
|
||||
<literal>DataAccessException</literal> hierarchy. The
|
||||
<literal>HibernateDaoSupport</literal> base class offers methods to
|
||||
access the current transactional <literal>Session</literal>
|
||||
and to convert exceptions in such a scenario; similar methods are also
|
||||
available as static helpers on the
|
||||
<classname>SessionFactoryUtils</classname> class. Note that such code
|
||||
<literal>SessionFactoryUtils</literal> class. Note that such code
|
||||
will usually pass '<literal>false</literal>' as the value of the
|
||||
<methodname>DoGetSession(..)</methodname> method's
|
||||
'<literal>allowCreate</literal>' argument, to enforce running within a
|
||||
transaction (which avoids the need to close the returned
|
||||
<interfacename>Session</interfacename>, as its lifecycle is managed by
|
||||
<literal>Session</literal>, as its lifecycle is managed by
|
||||
the transaction). Asking for the</para>
|
||||
|
||||
<programlisting>public class HibernateProductDao extends HibernateDaoSupport implements ProductDao {
|
||||
<programlisting language="csharp">public class HibernateProductDao extends HibernateDaoSupport implements ProductDao {
|
||||
|
||||
public Customer SaveOrUpdate(Customer customer)
|
||||
{
|
||||
@@ -527,18 +544,18 @@
|
||||
DataAccessException.</para>
|
||||
</section>
|
||||
|
||||
<section id="orm-hibernate-straight">
|
||||
<section xml:id="orm-hibernate-straight">
|
||||
<title>Implementing DAOs based on plain Hibernate 1.2 API</title>
|
||||
|
||||
<para>Hibernate 1.2 introduced a feature called "contextual Sessions",
|
||||
where Hibernate itself manages one current
|
||||
<interfacename>ISession</interfacename> per transaction. This is roughly
|
||||
<literal>ISession</literal> per transaction. This is roughly
|
||||
equivalent to Spring's synchronization of one Hibernate
|
||||
<interfacename>Session</interfacename> per transaction. A corresponding
|
||||
<literal>Session</literal> per transaction. A corresponding
|
||||
DAO implementation looks like as follows, based on the plain Hibernate
|
||||
API:</para>
|
||||
|
||||
<programlisting>public class ProductDaoImpl implements IProductDao {
|
||||
<programlisting language="csharp">public class ProductDaoImpl implements IProductDao {
|
||||
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
@@ -575,13 +592,13 @@ public class HibernateCustomerDao : ICustomerDao {
|
||||
|
||||
<para>The above DAO follows the Dependency Injection pattern: it fits
|
||||
nicely into a Spring IoC container, just like it would if coded against
|
||||
Spring's <classname>HibernateTemplate</classname>. Of course, such a DAO
|
||||
Spring's <literal>HibernateTemplate</literal>. Of course, such a DAO
|
||||
can also be set up in plain C# (for example, in unit tests): simply
|
||||
instantiate it and call <methodname>SessionFactory</methodname> property
|
||||
with the desired factory reference. As a Spring object definition, it
|
||||
would look as follows:</para>
|
||||
|
||||
<programlisting>
|
||||
<programlisting language="myxml">
|
||||
<objects>
|
||||
|
||||
<object id="CustomerDao" type="Spring.Northwind.Dao.NHibernate.HibernateCustomerDao, Spring.Northwind.Dao.NHibernate">
|
||||
@@ -602,9 +619,9 @@ public class HibernateCustomerDao : ICustomerDao {
|
||||
|
||||
<para>The first way is shown below</para>
|
||||
|
||||
<programlisting><object id="sessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
|
||||
<programlisting language="myxml"><object id="sessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
|
||||
|
||||
<emphasis role="bold"><property name="ExposeTransactionAwareSessionFactory" value="true" /></emphasis>
|
||||
<property name="ExposeTransactionAwareSessionFactory" value="true" />
|
||||
|
||||
<!-- other configuration settings omitted -->
|
||||
|
||||
@@ -612,7 +629,7 @@ public class HibernateCustomerDao : ICustomerDao {
|
||||
|
||||
<para>Which is simply a shortcut for the following configuration</para>
|
||||
|
||||
<programlisting><object id="sessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
|
||||
<programlisting language="myxml"><object id="sessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
|
||||
|
||||
<!-- other configuration settings omitted -->
|
||||
|
||||
@@ -621,8 +638,8 @@ public class HibernateCustomerDao : ICustomerDao {
|
||||
|
||||
<!-- other dictionary entries omitted -->
|
||||
|
||||
<emphasis role="bold"><entry key="hibernate.current_session_context_class"
|
||||
value="Spring.Data.NHibernate.SpringSessionContext, Spring.Data.NHibernate12"/></emphasis>
|
||||
<entry key="hibernate.current_session_context_class"
|
||||
value="Spring.Data.NHibernate.SpringSessionContext, Spring.Data.NHibernate12"/>
|
||||
|
||||
</dictionary>
|
||||
</property>
|
||||
@@ -635,7 +652,7 @@ public class HibernateCustomerDao : ICustomerDao {
|
||||
doubt feel more natural to Hibernate developers.</para>
|
||||
|
||||
<para>However, the DAO throws plain
|
||||
<classname>HibernateException</classname> which means that callers can
|
||||
<literal>HibernateException</literal> which means that callers can
|
||||
only treat exceptions as generally fatal - unless they want to depend on
|
||||
Hibernate's own exception hierarchy. Catching specific causes such as an
|
||||
optimistic locking failure is not possible without tying the caller to
|
||||
@@ -644,11 +661,11 @@ public class HibernateCustomerDao : ICustomerDao {
|
||||
special exception treatment.</para>
|
||||
|
||||
<para>Fortunately, Spring's
|
||||
<classname>LocalSessionFactoryObject</classname> supports Hibernate's
|
||||
<literal>LocalSessionFactoryObject</literal> supports Hibernate's
|
||||
<methodname>SessionFactory.GetCurrentSession()</methodname> method for
|
||||
any Spring transaction strategy, returning the current Spring-managed
|
||||
transactional <interfacename>Session</interfacename> even with
|
||||
<classname>HibernateTransactionManager</classname>.</para>
|
||||
transactional <literal>Session</literal> even with
|
||||
<literal>HibernateTransactionManager</literal>.</para>
|
||||
|
||||
<para>In summary: DAOs can be implemented based on the plain Hibernate
|
||||
1.2 API, while still being able to participate in Spring-managed
|
||||
@@ -656,29 +673,29 @@ public class HibernateCustomerDao : ICustomerDao {
|
||||
transaction.</para>
|
||||
</section>
|
||||
|
||||
<section id="orm-hibernate-tx-programmatic">
|
||||
<section xml:id="orm-hibernate-tx-programmatic">
|
||||
<title>Programmatic transaction demarcation</title>
|
||||
|
||||
<para>Transactions can be demarcated in a higher level of the
|
||||
application, on top of such lower-level data access services spanning
|
||||
any number of operations. There are no restrictions on the
|
||||
implementation of the surrounding business service here as well, it just
|
||||
needs a Spring <classname>PlatformTransactionManager</classname>. Again,
|
||||
needs a Spring <literal>PlatformTransactionManager</literal>. Again,
|
||||
the latter can come from anywhere, but preferably as an object reference
|
||||
via a <methodname>TransactionManager</methodname> property - just like
|
||||
the <classname>productDAO</classname> should be set via a
|
||||
the <literal>productDAO</literal> should be set via a
|
||||
<methodname>setProductDao(..)</methodname> method. The following
|
||||
snippets show a transaction manager and a business service definition in
|
||||
a Spring application context, and an example for a business method
|
||||
implementation.</para>
|
||||
|
||||
<programlisting><objects>
|
||||
<programlisting language="myxml"><objects>
|
||||
|
||||
TO BE DONE
|
||||
|
||||
</objects></programlisting>
|
||||
|
||||
<programlisting>public class FulfillmentService : IFulfillmentService
|
||||
<programlisting language="csharp">public class FulfillmentService : IFulfillmentService
|
||||
|
||||
private TransactionTemplate transactionTemplate;
|
||||
|
||||
@@ -697,7 +714,7 @@ TO BE DONE
|
||||
}</programlisting>
|
||||
</section>
|
||||
|
||||
<section id="orm-hibernate-tx-declarative">
|
||||
<section xml:id="orm-hibernate-tx-declarative">
|
||||
<title>Declarative transaction demarcation</title>
|
||||
|
||||
<para>Alternatively, one can use Spring's declarative transaction
|
||||
@@ -711,7 +728,7 @@ TO BE DONE
|
||||
<para>An example showing attribute driven transaction is shown
|
||||
below</para>
|
||||
|
||||
<programlisting><objects>
|
||||
<programlisting language="myxml"><objects>
|
||||
|
||||
<object id="HibernateTransactionManager"
|
||||
type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate">
|
||||
@@ -740,7 +757,7 @@ TO BE DONE
|
||||
expresses the intent as compared to the contents of
|
||||
DeclarativeServicesAttributeDriven.xml.</para>
|
||||
|
||||
<programlisting><objects xmlns="http://www.springframework.net"
|
||||
<programlisting language="myxml"><objects xmlns="http://www.springframework.net"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:tx="http://www.springframework.net/schema/tx"
|
||||
xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/schema/objects/spring-objects.xsd
|
||||
@@ -772,7 +789,7 @@ TO BE DONE
|
||||
<para>The placement of the transaction attribute in the service layer
|
||||
method is shown below.</para>
|
||||
|
||||
<programlisting>public class FulfillmentService : IFulfillmentService
|
||||
<programlisting language="csharp">public class FulfillmentService : IFulfillmentService
|
||||
{
|
||||
// fields and properties for dao object omitted, see above
|
||||
|
||||
@@ -808,7 +825,7 @@ TO BE DONE
|
||||
boundaries, you can import a configuration file with the following XML
|
||||
instead of using <tx:attribute-driven/></para>
|
||||
|
||||
<programlisting> <object id="TxProxyConfigurationTemplate" abstract="true"
|
||||
<programlisting language="myxml"> <object id="TxProxyConfigurationTemplate" abstract="true"
|
||||
type="Spring.Transaction.Interceptor.TransactionProxyFactoryObject, Spring.Data">
|
||||
|
||||
<property name="PlatformTransactionManager" ref="HibernateTransactionManager"/>
|
||||
@@ -825,23 +842,23 @@ TO BE DONE
|
||||
configuration of other features, such as rollback rules.</para>
|
||||
</section>
|
||||
|
||||
<section id="orm-hibernate-tx-strategies">
|
||||
<section xml:id="orm-hibernate-tx-strategies">
|
||||
<title>Transaction management strategies</title>
|
||||
|
||||
<para>Both <classname>TransactionTemplate</classname> and
|
||||
<classname>TransactionInterceptor</classname> (not yet seen explicitly
|
||||
<para>Both <literal>TransactionTemplate</literal> and
|
||||
<literal>TransactionInterceptor</literal> (not yet seen explicitly
|
||||
in above configuration, TransactionProxyFactoryObject uses a
|
||||
TransactionInterceptor, you would have to specify it explicitly if you
|
||||
were using an ordinary ProxyFactoryObject.) delegate the actual
|
||||
transaction handling to a
|
||||
<classname>PlatformTransactionManager</classname> instance, which can be
|
||||
a <classname>HibernateTransactionManager</classname> (for a single
|
||||
Hibernate <interfacename>SessionFactory</interfacename>, using a
|
||||
<classname>ThreadLocal</classname>
|
||||
<interfacename>Session</interfacename> under the hood) or a
|
||||
<classname>TxScopeTransactionManager</classname> (delegating to MS-DTC
|
||||
<literal>PlatformTransactionManager</literal> instance, which can be
|
||||
a <literal>HibernateTransactionManager</literal> (for a single
|
||||
Hibernate <literal>SessionFactory</literal>, using a
|
||||
<literal>ThreadLocal</literal>
|
||||
<literal>Session</literal> under the hood) or a
|
||||
<literal>TxScopeTransactionManager</literal> (delegating to MS-DTC
|
||||
for distributed transaction) for Hibernate applications. You could even
|
||||
use a custom <classname>PlatformTransactionManager</classname>
|
||||
use a custom <literal>PlatformTransactionManager</literal>
|
||||
implementation. So switching from native Hibernate transaction
|
||||
management to TxScopeTransactionManager, such as when facing distributed
|
||||
transaction requirements for certain deployments of your application, is
|
||||
@@ -852,24 +869,24 @@ TO BE DONE
|
||||
|
||||
<para>For distributed transactions across multiple Hibernate session
|
||||
factories, simply combine
|
||||
<classname>TxScopeTransactionManager</classname> as a transaction
|
||||
strategy with multiple <classname>LocalSessionFactoryObject</classname>
|
||||
<literal>TxScopeTransactionManager</literal> as a transaction
|
||||
strategy with multiple <literal>LocalSessionFactoryObject</literal>
|
||||
definitions. Each of your DAOs then gets one specific
|
||||
<interfacename>SessionFactory</interfacename> reference passed into it's
|
||||
<literal>SessionFactory</literal> reference passed into it's
|
||||
respective object property.</para>
|
||||
|
||||
<programlisting>TO BE DONE
|
||||
</programlisting>
|
||||
|
||||
<para><classname>HibernateTransactionManager</classname> can export the
|
||||
ADO.NET <interfacename>Transaction</interfacename> used by Hibernate to
|
||||
<para><literal>HibernateTransactionManager</literal> can export the
|
||||
ADO.NET <literal>Transaction</literal> used by Hibernate to
|
||||
plain ADO.NET access code, for a specific
|
||||
<interfacename>DbProvider</interfacename>. (matching connection string).
|
||||
<literal>DbProvider</literal>. (matching connection string).
|
||||
This allows for high-level transaction demarcation with mixed
|
||||
Hibernate/ADO.NET data access!</para>
|
||||
</section>
|
||||
|
||||
<section id="orm-hibernate-web">
|
||||
<section xml:id="orm-hibernate-web">
|
||||
<title>Web Session Management</title>
|
||||
|
||||
<para>The open session in view pattern keeps the hibernate session open
|
||||
@@ -877,7 +894,7 @@ TO BE DONE
|
||||
displayed. You configure its use by adding an additional custom HTTP
|
||||
module declaration as shown below</para>
|
||||
|
||||
<programlisting> <system.web>
|
||||
<programlisting language="myxml"> <system.web>
|
||||
<httpModules>
|
||||
<add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate"/>
|
||||
</httpModules>
|
||||
@@ -890,7 +907,7 @@ TO BE DONE
|
||||
will use by setting 'global' application key-value pairs as shown below.
|
||||
(this will change in future releases)</para>
|
||||
|
||||
<programlisting> <appSettings>
|
||||
<programlisting language="myxml"> <appSettings>
|
||||
<add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="SessionFactory"/>
|
||||
</appSettings></programlisting>
|
||||
|
||||
@@ -933,7 +950,7 @@ TO BE DONE
|
||||
you to use a single NHibernate session across multiple transactions. The
|
||||
usage is shown below</para>
|
||||
|
||||
<programlisting>using (new SessionScope())
|
||||
<programlisting language="csharp">using (new SessionScope())
|
||||
{
|
||||
... do multiple operations with a single session, possibly in multiple transactions.
|
||||
}</programlisting>
|
||||
|
||||
Reference in New Issue
Block a user