From 8ebf5eab9023721c88d01f00621eec3224b3d5b4 Mon Sep 17 00:00:00 2001 From: lahma Date: Mon, 21 Mar 2011 06:56:51 +0000 Subject: [PATCH] SPRNET-1422: HibernateTemplate should support Merge --- .../Data/NHibernate/HibernateTemplate.cs | 57 ++++++++++++++++++- .../NHibernate/ICommonHibernateOperations.cs | 27 +++++++-- .../NHibernate/Generic/HibernateTemplate.cs | 21 +++++++ .../Spring.Data.NHibernate12.2003.csproj | 4 +- .../Spring.Data.NHibernate12.2005.csproj | 4 +- .../Spring.Data.NHibernate12.2008.csproj | 4 +- .../Spring.Data.NHibernate12.2010.csproj | 4 +- .../Spring.Data.NHibernate12.build | 2 +- 8 files changed, 105 insertions(+), 18 deletions(-) diff --git a/src/Spring/Spring.Data.NHibernate/Data/NHibernate/HibernateTemplate.cs b/src/Spring/Spring.Data.NHibernate/Data/NHibernate/HibernateTemplate.cs index 3f9c7236..ab2cccfb 100644 --- a/src/Spring/Spring.Data.NHibernate/Data/NHibernate/HibernateTemplate.cs +++ b/src/Spring/Spring.Data.NHibernate/Data/NHibernate/HibernateTemplate.cs @@ -677,8 +677,28 @@ namespace Spring.Data.NHibernate public object SaveOrUpdateCopy(object entity) { return Execute(new SaveOrUpdateCopyHibernateCallback(this, entity),true); + } + +#if !NH_1_2 + /// + /// Copy the state of the given object onto the persistent object with the same identifier. + /// If there is no persistent instance currently associated with the session, it will be loaded. + /// Return the persistent instance. If the given instance is unsaved, + /// save a copy of and return it as a newly persistent instance. + /// The given instance does not become associated with the session. + /// This operation cascades to associated instances if the association is mapped with cascade="merge". + /// The semantics of this method are defined by JSR-220. + /// + /// The persistent object to merge. + /// (not necessarily to be associated with the Hibernate Session) + /// + /// An updated persistent instance + /// In case of Hibernate errors + public object Merge(object entity) + { + return Execute(new MergeHibernateCallback(this, entity), true); } - +#endif /// /// Remove all objects from the Session cache, and cancel all pending saves, @@ -2226,10 +2246,41 @@ namespace Spring.Data.NHibernate public object DoInHibernate(ISession session) { outer.CheckWriteOperationAllowed(session); - return session.SaveOrUpdateCopy(entity); + return session.SaveOrUpdateCopy(entity); } + } + +#if !NH_1_2 + internal class MergeHibernateCallback : IHibernateCallback + { + private HibernateTemplate outer; + private object entity; + + public MergeHibernateCallback(HibernateTemplate template, object entity) + { + this.outer = template; + this.entity = entity; + } + + /// + /// Gets called by HibernateTemplate with an active + /// Hibernate Session. Does not need to care about activating or closing + /// the Session, or handling transactions. + /// + /// + ///

+ /// Allows for returning a result object created within the callback, i.e. + /// a domain object or a collection of domain objects. Note that there's + /// special support for single step actions: see HibernateTemplate.find etc. + ///

+ ///
+ public object DoInHibernate(ISession session) + { + outer.CheckWriteOperationAllowed(session); + return session.Merge(entity); + } } - +#endif #endregion diff --git a/src/Spring/Spring.Data.NHibernate/Data/NHibernate/ICommonHibernateOperations.cs b/src/Spring/Spring.Data.NHibernate/Data/NHibernate/ICommonHibernateOperations.cs index 2541ee04..332fc716 100644 --- a/src/Spring/Spring.Data.NHibernate/Data/NHibernate/ICommonHibernateOperations.cs +++ b/src/Spring/Spring.Data.NHibernate/Data/NHibernate/ICommonHibernateOperations.cs @@ -234,11 +234,26 @@ namespace Spring.Data.NHibernate /// The actually associated persistent object. /// (either an already loaded instance with the same id, or the given object) /// In case of Hibernate errors - object SaveOrUpdateCopy(object entity); - - - #endregion - - + object SaveOrUpdateCopy(object entity); + +#if !NH_1_2 + /// + /// Copy the state of the given object onto the persistent object with the same identifier. + /// If there is no persistent instance currently associated with the session, it will be loaded. + /// Return the persistent instance. If the given instance is unsaved, + /// save a copy of and return it as a newly persistent instance. + /// The given instance does not become associated with the session. + /// This operation cascades to associated instances if the association is mapped with cascade="merge". + /// The semantics of this method are defined by JSR-220. + /// + /// The persistent object to merge. + /// (not necessarily to be associated with the Hibernate Session) + /// + /// An updated persistent instance + /// In case of Hibernate errors + object Merge(object entity); +#endif + + #endregion } } diff --git a/src/Spring/Spring.Data.NHibernate12/Data/NHibernate/Generic/HibernateTemplate.cs b/src/Spring/Spring.Data.NHibernate12/Data/NHibernate/Generic/HibernateTemplate.cs index 9f168b28..15bc727a 100644 --- a/src/Spring/Spring.Data.NHibernate12/Data/NHibernate/Generic/HibernateTemplate.cs +++ b/src/Spring/Spring.Data.NHibernate12/Data/NHibernate/Generic/HibernateTemplate.cs @@ -603,6 +603,27 @@ namespace Spring.Data.NHibernate.Generic return classicHibernateTemplate.SaveOrUpdateCopy(entity); } +#if !NH_1_2 + /// + /// Copy the state of the given object onto the persistent object with the same identifier. + /// If there is no persistent instance currently associated with the session, it will be loaded. + /// Return the persistent instance. If the given instance is unsaved, + /// save a copy of and return it as a newly persistent instance. + /// The given instance does not become associated with the session. + /// This operation cascades to associated instances if the association is mapped with cascade="merge". + /// The semantics of this method are defined by JSR-220. + /// + /// The persistent object to merge. + /// (not necessarily to be associated with the Hibernate Session) + /// + /// An updated persistent instance + /// In case of Hibernate errors + public object Merge(object entity) + { + return classicHibernateTemplate.Merge(entity); + } +#endif + #endregion #region IHibernateOperations Gets diff --git a/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2003.csproj b/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2003.csproj index 11c6f80b..a33d116f 100644 --- a/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2003.csproj +++ b/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2003.csproj @@ -28,7 +28,7 @@ BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" - DefineConstants = "DEBUG;TRACE;NET_1_1" + DefineConstants = "DEBUG;TRACE;NET_1_1,NH_1_2" DocumentationFile = "" DebugSymbols = "true" FileAlignment = "4096" @@ -48,7 +48,7 @@ BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" - DefineConstants = "TRACE" + DefineConstants = "TRACE,NH_1_2" DocumentationFile = "" DebugSymbols = "false" FileAlignment = "4096" diff --git a/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2005.csproj b/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2005.csproj index b97f4698..85d413cd 100644 --- a/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2005.csproj +++ b/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2005.csproj @@ -15,7 +15,7 @@ full false ..\..\..\build\VS.Net.2005\Spring.Data.NHibernate12\Debug\ - TRACE;DEBUG;NET_2_0 + TRACE;DEBUG;NET_2_0,NH_1_2 prompt 4 Spring.Data.NHibernate12.xml @@ -24,7 +24,7 @@ pdbonly true ..\..\..\build\VS.Net.2005\Spring.Data.NHibernate12\Release\ - TRACE;NET_2_0 + TRACE;NET_2_0,NH_1_2 prompt 4 diff --git a/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2008.csproj b/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2008.csproj index 238c073b..c1db179d 100644 --- a/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2008.csproj +++ b/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2008.csproj @@ -16,7 +16,7 @@ full false ..\..\..\build\VS.Net.2008\Spring.Data.NHibernate12\Debug\ - TRACE;DEBUG;NET_2_0 + TRACE;DEBUG;NET_2_0,NH_1_2 prompt 4 Spring.Data.NHibernate12.xml @@ -25,7 +25,7 @@ pdbonly true ..\..\..\build\VS.Net.2008\Spring.Data.NHibernate12\Release\ - TRACE;NET_2_0 + TRACE;NET_2_0,NH_1_2 prompt 4 diff --git a/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2010.csproj b/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2010.csproj index 449472ad..b8b591f6 100644 --- a/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2010.csproj +++ b/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.2010.csproj @@ -21,7 +21,7 @@ full false ..\..\..\build\VS.Net.2010\Spring.Data.NHibernate12\Debug\ - TRACE;DEBUG;NET_2_0 + TRACE;DEBUG;NET_2_0,NH_1_2 prompt 4 Spring.Data.NHibernate12.xml @@ -30,7 +30,7 @@ pdbonly true ..\..\..\build\VS.Net.2010\Spring.Data.NHibernate12\Release\ - TRACE;NET_2_0 + TRACE;NET_2_0,NH_1_2 prompt 4 diff --git a/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.build b/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.build index 2e5f3056..da3ba464 100644 --- a/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.build +++ b/src/Spring/Spring.Data.NHibernate12/Spring.Data.NHibernate12.build @@ -17,7 +17,7 @@ -