From: <fab...@us...> - 2009-06-02 23:09:15
|
Revision: 4402 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4402&view=rev Author: fabiomaulo Date: 2009-06-02 23:09:13 +0000 (Tue, 02 Jun 2009) Log Message: ----------- Fix NH-1517 Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Event/Default/DefaultMergeEventListener.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/Classic/EntityWithLifecycle.cs trunk/nhibernate/src/NHibernate.Test/Classic/EntityWithLifecycle.hbm.xml trunk/nhibernate/src/NHibernate.Test/Classic/LifecycleFixture.cs Modified: trunk/nhibernate/src/NHibernate/Event/Default/DefaultMergeEventListener.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Event/Default/DefaultMergeEventListener.cs 2009-06-01 19:58:58 UTC (rev 4401) +++ trunk/nhibernate/src/NHibernate/Event/Default/DefaultMergeEventListener.cs 2009-06-02 23:09:13 UTC (rev 4402) @@ -1,6 +1,7 @@ using System; using System.Collections; using log4net; +using NHibernate.Classic; using NHibernate.Engine; using NHibernate.Intercept; using NHibernate.Persister.Entity; @@ -230,6 +231,12 @@ } else { + // NH different behavior : NH-1517 + if (InvokeUpdateLifecycle(entity, persister, source)) + { + return; + } + copyCache[entity] = result; //before cascade! object target = source.PersistenceContext.Unproxy(result); @@ -263,6 +270,20 @@ } } + protected virtual bool InvokeUpdateLifecycle(object entity, IEntityPersister persister, IEventSource source) + { + if (persister.ImplementsLifecycle(source.EntityMode)) + { + log.Debug("calling onUpdate()"); + if (((ILifecycle)entity).OnUpdate(source) == LifecycleVeto.Veto) + { + log.Debug("update vetoed by onUpdate()"); + return true; + } + } + return false; + } + private void MarkInterceptorDirty(object entity, object target) { if (FieldInterceptionHelper.IsInstrumented(entity)) Added: trunk/nhibernate/src/NHibernate.Test/Classic/EntityWithLifecycle.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Classic/EntityWithLifecycle.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/Classic/EntityWithLifecycle.cs 2009-06-02 23:09:13 UTC (rev 4402) @@ -0,0 +1,63 @@ +using System.Collections.Generic; +using NHibernate.Classic; + +namespace NHibernate.Test.Classic +{ + public class EntityWithLifecycle : ILifecycle + { + public virtual int Id { get; set; } + public virtual string Name { get; set; } + public virtual double Heigth { get; set; } + public virtual double Width { get; set; } + public EntityWithLifecycle() {} + public EntityWithLifecycle(string name, double heigth, double width) + { + Name = name; + Heigth = heigth; + Width = width; + } + + public virtual LifecycleVeto OnSave(ISession s) + { + return IsValid() ? LifecycleVeto.NoVeto : LifecycleVeto.Veto; + } + + public virtual LifecycleVeto OnUpdate(ISession s) + { + return IsValid() ? LifecycleVeto.NoVeto : LifecycleVeto.Veto; + } + + public virtual LifecycleVeto OnDelete(ISession s) + { + return IsValid() ? LifecycleVeto.NoVeto : LifecycleVeto.Veto; + } + + public virtual void OnLoad(ISession s, object id) + { + // nothing to do + } + + public virtual IList<string> GetBrokenRules() + { + IList<string> result = new List<string>(3); + if (string.IsNullOrEmpty(Name) || Name.Trim().Length < 2) + result.Add("The Name must have more than one char."); + if (Heigth <= 0) + result.Add("Heigth must be great than 0"); + if (Width <= 0) + result.Add("Width must be great than 0."); + return result; + } + + /// <summary> + /// Validate the state of the object before persisting it. If a violation occurs, + /// throw a <see cref="ValidationFailure" />. This method must not change the state of the object + /// by side-effect. + /// </summary> + private bool IsValid() + { + IList<string> br = GetBrokenRules(); + return br == null || br.Count == 0; + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/Classic/EntityWithLifecycle.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Classic/EntityWithLifecycle.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/Classic/EntityWithLifecycle.hbm.xml 2009-06-02 23:09:13 UTC (rev 4402) @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" + namespace="NHibernate.Test.Classic" + assembly="NHibernate.Test" > + <class name="EntityWithLifecycle"> + <id name="Id"> + <generator class="hilo" /> + </id> + <property name="Name" type="string" length="40"/> + <property name="Heigth" type="double"/> + <property name="Width" type="double"/> + </class> +</hibernate-mapping> Added: trunk/nhibernate/src/NHibernate.Test/Classic/LifecycleFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Classic/LifecycleFixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/Classic/LifecycleFixture.cs 2009-06-02 23:09:13 UTC (rev 4402) @@ -0,0 +1,134 @@ +using System.Collections; +using NHibernate.Cfg; +using NUnit.Framework; + +namespace NHibernate.Test.Classic +{ + [TestFixture] + public class LifecycleFixture : TestCase + { + protected override string MappingsAssembly + { + get { return "NHibernate.Test"; } + } + + protected override IList Mappings + { + get { return new[] { "Classic.EntityWithLifecycle.hbm.xml" }; } + } + + protected override void Configure(Configuration configuration) + { + configuration.SetProperty(Environment.GenerateStatistics, "true"); + } + + [Test] + public void Save() + { + sessions.Statistics.Clear(); + using (ISession s = OpenSession()) + { + s.Save(new EntityWithLifecycle()); + s.Flush(); + } + Assert.That(sessions.Statistics.EntityInsertCount, Is.EqualTo(0)); + + var v = new EntityWithLifecycle("Shinobi", 10, 10); + using (ISession s = OpenSession()) + { + s.Save(v); + s.Delete(v); + s.Flush(); + } + } + + [Test] + public void Update() + { + var v = new EntityWithLifecycle("Shinobi", 10, 10); + using (ISession s = OpenSession()) + { + s.Save(v); + s.Flush(); + } + + // update detached + sessions.Statistics.Clear(); + v.Heigth = 0; + using (ISession s = OpenSession()) + { + s.Update(v); + s.Flush(); + } + Assert.That(sessions.Statistics.EntityUpdateCount, Is.EqualTo(0)); + + // cleanup + using (ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + s.CreateQuery("delete from EntityWithLifecycle").ExecuteUpdate(); + tx.Commit(); + } + } + + [Test] + public void SaveOrUpdateCopy() + { + var v = new EntityWithLifecycle("Shinobi", 10, 10); + using (ISession s = OpenSession()) + { + s.Save(v); + s.Flush(); + } + v.Heigth = 0; + sessions.Statistics.Clear(); + using (ISession s = OpenSession()) + { + s.SaveOrUpdateCopy(v); + s.Flush(); + } + Assert.That(sessions.Statistics.EntityUpdateCount, Is.EqualTo(0)); + + var v1 = new EntityWithLifecycle("Shinobi", 0, 10); + using (ISession s = OpenSession()) + { + s.SaveOrUpdateCopy(v1); + s.Flush(); + } + Assert.That(sessions.Statistics.EntityInsertCount, Is.EqualTo(0)); + Assert.That(sessions.Statistics.EntityUpdateCount, Is.EqualTo(0)); + + + // cleanup + using (ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + s.CreateQuery("delete from EntityWithLifecycle").ExecuteUpdate(); + tx.Commit(); + } + } + + [Test] + public void Delete() + { + var v = new EntityWithLifecycle("Shinobi", 10, 10); + using (ISession s = OpenSession()) + { + s.Save(v); + s.Flush(); + sessions.Statistics.Clear(); + v.Heigth = 0; + s.Delete(v); + s.Flush(); + Assert.That(sessions.Statistics.EntityDeleteCount, Is.EqualTo(0)); + } + + using (ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + s.CreateQuery("delete from EntityWithLifecycle").ExecuteUpdate(); + tx.Commit(); + } + } + } +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-06-01 19:58:58 UTC (rev 4401) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-06-02 23:09:13 UTC (rev 4402) @@ -110,6 +110,8 @@ <Compile Include="CfgTest\LocatedInTestAssembly.cs" /> <Compile Include="CfgTest\MappingDocumentAggregatorTests.cs" /> <Compile Include="CfgTest\MappingDocumentParserTests.cs" /> + <Compile Include="Classic\EntityWithLifecycle.cs" /> + <Compile Include="Classic\LifecycleFixture.cs" /> <Compile Include="Classic\ValidatableFixture.cs" /> <Compile Include="Classic\Video.cs" /> <Compile Include="CollectionTest\A.cs" /> @@ -1851,6 +1853,7 @@ <EmbeddedResource Include="Ado\VerySimple.hbm.xml" /> <EmbeddedResource Include="Ado\AlmostSimple.hbm.xml" /> <EmbeddedResource Include="CacheTest\EntityWithFilters.xml" /> + <EmbeddedResource Include="Classic\EntityWithLifecycle.hbm.xml" /> <Content Include="DynamicEntity\package.html" /> <EmbeddedResource Include="NHSpecificTest\NH1757\Mappings.hbm.xml" /> <EmbeddedResource Include="Events\PostEvents\SimpleEntity.hbm.xml" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |