I am trying to implement a simple audit on my objects. Audited objects implement an interface ITimestampablethat defines the properties of DateAddedand DateModified.
I created and registered an event listener to populate these values. Here is the complete code.
internal class TimeStampEventListener : IPreUpdateEventListener, IPreInsertEventListener
{
public bool OnPreUpdate(PreUpdateEvent e)
{
if (e.Entity is ITimestampable)
{
(e.Entity as ITimestampable).DateModified = DateTime.Now;
}
return false;
}
public bool OnPreInsert(PreInsertEvent e)
{
if (e.Entity is ITimestampable)
{
(e.Entity as ITimestampable).DateAdded = DateTime.Now;
}
return false;
}
public void Register(Configuration configuration)
{
configuration.SetListener(ListenerType.PreInsert, this);
configuration.SetListener(ListenerType.PreUpdate, this);
}
}
Now, when I make a flush session, the listener is called, the audit properties are set correctly, but most of the time they are not stored in the database. “Most of the time,” I mean, very rarely, values are actually preserved. I'm not sure, but this seems like the first insert / update after the application starts, which is even weirder.
Of course, at first I make changes to the entity, this change is saved, but the audit property is not.
SQL , , NULL , , . Btw. MySQL, DateAdded DateModified DATE.
NHibernate <property>. , - "" ...?
. .