From: Scott F. (JIRA) <nh...@gm...> - 2011-04-06 07:56:52
|
[ http://216.121.112.228/browse/NH-2596?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=20835#action_20835 ] Scott Findlater commented on NH-2596: ------------------------------------- Fabio, I rasied NH-2617 which was closed with this being a duplicate - sorry about that. However, I still don't feel I have a conclusion. You say "pre-insert and pre-update listeners are not intended to be used to change the values of the entity. Instead they should be used to check-values (for that reason they return "veto")." but how come if an inherited entity which has just 1 property modified prior to the PreUpdate listener, will have all modifications made within the PreUpdate listener persisted to the database (even with dynamic-update set to true) vs an inherited entity which only has properties modified within the PreUpdate listener will not result in changes being persisted? Does this behaviour not demonstrate an issue in NHibernate where entity's modified within PreUpdate listeners can change data and behave correctly? Also Ayende's very popular audit trail blog post which uses PreUpdate and PreInsert listeners (http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx) - does that mean this approach is wrong? This is not intended to be a complaint at NHibernate, just I really want to understand the event model correctly. > changes made in IPreInsert/UpdateEventListener are not persisted into DB on inherited classes > --------------------------------------------------------------------------------------------- > > Key: NH-2596 > URL: http://216.121.112.228/browse/NH-2596 > Project: NHibernate > Issue Type: Bug > Components: Core > Affects Versions: 3.0.0.Alpha3 > Reporter: Filip Kinsky > Priority: Major > Attachments: NHListenerTest.zip, NHListenerTest_NH31.zip > > > We're logging modification date in few of our persisted classes using simple > interface and basic IPreInsert/UpdateEventListener implementation which uses > this code to set LastModified field: > var trackable = entity as ITrackModificationDate; > if (trackable != null) > { > trackable.LastModified = DateTime.Now; > persister.SetPropertyValue(state, "LastModified", trackable.LastModified); > } > This works for basic classes without any inheritance hierarchy, but when I > map inherited classes (LastModified field implemented by the hierarchy > root) and update some property which exists just in the inherited class, the > listener is triggered, but the modified LastModified property value is not > being persisted into database. I isolated the problem and implemented > attached failing test, but I'm not able to figure out what should I do in > the listener to force NH to update the DB properly. > Test classes: > public class Thing : ITrackModificationDate > { > public virtual long Id { get; set; } > public virtual DateTime LastModified { get; set; } > } > public class InheritedThing: Thing > { > public virtual string SomeText { get; set; } > } > public class ThingMap : ClassMap<Thing> > { > public ThingMap() > { > Id(x => x.Id).GeneratedBy.Assigned(); > Map(x => x.LastModified); > } > } > public class InheritedThingMap: SubclassMap<InheritedThing> > { > public InheritedThingMap() > { > Map(x => x.SomeText); > } > } > Failing test: > [Fact] > public void InheritedThing_LastModified_Should_BeSetOnUpdate() > { > var t = new InheritedThing {Id = 1, SomeText = "aa"}; > session.Save(t); > session.Flush(); > session.Clear(); > > Thread.Sleep(1000); > t = session.Get<InheritedThing>(1L); > t.SomeText = "bb"; > session.Update(t); > session.Flush(); > session.Clear(); > > t = session.Get<InheritedThing>(1L); > Assert.True(DateTime.Now.Subtract(t.LastModified).TotalSeconds < 1); //this fails - LastModified property isn't updated in DB > } -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://216.121.112.228/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira |