The initial problem is in detail described on
http://216.121.112.228/browse/NH-2596 . In short - I need to implement last modification time tracking for my entities, but IPreInsert/UpdateEventListener approach which is suggested on many places doesn't fully work. The problem is mainly with dirty entities flushed implicitly using NH dirty entities tracking like this:
using(var session = fac.OpenSession())
{
var e = session.Load<MyEntity>(123);
e.Value++;
}
In this case IPreInsert/UpdateEventListener aren't fired at all and so the modification time isn't logged. I was suggested by Fabio to use IFlushEntityEventListener for this scenario, which I just did, but I face another problem now - "HibernateException: Found shared references to a collection" when I try to update any entity which contains a mapped collection. My IFlushEntityEventListener implementation looks like this:
public class SetModificationDateListener: DefaultFlushEntityEventListener
{
public CurrentDateTimeDelegate CurrentDateTime { get; set; }
private void SetModificationDateIfPossible(object entity)
{
var trackable = entity as ITrackModificationDate;
if (trackable != null)
{
trackable.LastModified = CurrentDateTime();
}
}
public override void OnFlushEntity(FlushEntityEvent @event)
{
if (@event.EntityEntry.Status != Status.Deleted &&
(!@event.EntityEntry.ExistsInDatabase ||@event.Session.IsDirtyEntity(@event.Entity)))
{
SetModificationDateIfPossible(@event.Entity);
}
base.OnFlushEntity(@event);
}
}
The exception occurs on base.OnFlushEntity(@event) call. I was already trying to implement just pure IFlushEntityEventListener, but it looks like it disables default IFlushEntityEventListener and no changes are persisted to DB at all.
Anybody has any suggestion what should I try to create a mechanism for modification time tracking in NH? IPreInsert/UpdateEventListener approach is widely suggested on many places, but it obviously doesn't fully work.