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. Did anyone face problem
like this before?
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
}
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.