|
From: Filip K. (JIRA) <nh...@gm...> - 2011-03-28 19:38:28
|
[ http://216.121.112.228/browse/NH-2596?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=20780#action_20780 ]
Filip Kinsky commented on NH-2596:
----------------------------------
The suggested solution unfortunately works just when entity is saved explicitely using Session.Save/Update/SaveOrUpdate. It doesn't work in case the entity is tracked by the session and saved implicitely on session.Flush like in this case:
using(var session = sessionFac.OpenSession())
{
var e = session.Load<MyEntity>(123);
e.Name = "new name";
}
Save/Update/SaveOrUpdateEventListeners aren't fired in this scenario AFAIK.
So there's no fully working solution to the problem at the moment in my opinion if I'm not wrong?
> 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
|