tags:

views:

234

answers:

1

I use the NHibernate OnPreInsert and OnPreUpdate events in a PreSaveEventListener to set the CreatedDate and ModifiedDate of my entities. The problem is, there are two entities for which both events get triggered when I first create them. This causes an issue because the entity state does not get saved after the OnPreInsert event, so the OnPreUpdate event operates on a whole new entity state and my CreatedDate never gets set (defaults to 01/01/0001).

At first, I thought this was because my code that was initiating two SaveOrUpdate calls back to back before the end of the transaction. Sure enough, I found some code to this effect. But then I realized this was still happening for the other entity. So far as I can tell, only these two entities have this issue. I temporarily solved the problem by setting the CreatedDate in their constructors, but I want to avoid this.

Here's my structure:
Business entity (an abstract class that has two concrete joined-subclasses)
BusinessContact entity with a Many-To-One relationship with Business

EDIT: I have recently realized that it's also happening on one other object (InvoiceLineItem), but not a near identical object (BillLineItem) instantiated and used in near identical ways. Seems rather arbitrary.

Has anyone seen this before?

Here's the event listener code:

public class PreSaveEventListener : IPreInsertEventListener, IPreUpdateEventListener {
    public bool OnPreInsert(PreInsertEvent @event) {
        EntityWithGuidId entity = @event.Entity as EntityWithGuidId;

        if (null != entity) {
            var createdDate = DateTime.Now;
            var modifiedDate= DateTime.Now;
            Set(@event.Persister, @event.State, "CreatedDate", createdDate);
            Set(@event.Persister, @event.State, "ModifiedDate", modifiedDate);
            entity.CreatedDate = createdDate;
            entity.ModifiedDate = modifiedDate;
        }

        return false;
    }

    public bool OnPreUpdate(PreUpdateEvent @event) {
        EntityWithGuidId entity = @event.Entity as EntityWithGuidId;

        if (null != entity) {
            var modifiedDate= DateTime.Now;
            Set(@event.Persister, @event.State, "ModifiedDate", modifiedDate);
            entity.ModifiedDate = modifiedDate;
        }

        return false;
    }

    private void Set(IEntityPersister persister, object[] state, string propertyName, object value) {
        var index = Array.IndexOf(persister.PropertyNames, propertyName);
        if (index == -1)
            return;
        state[index] = value;
    }
}
A: 

Event listeners caused a lot of different issues in my project and many of them doesn't make sense to me. I think your issue can be caused in case when NHibernate really updates your entity after it created. NHibernate can update version of entity or set some id (or guid) for it. Can you put here mapping of issued entity? I also will suggest you to look at sql queries in profiler.

MeF Corvi
I still haven't found the answer, but your attempt was the best (and only).
Chris F