views:

170

answers:

1

Hi,

I have an event listener (for Audit Logs) which needs to append audit log entries to the association of the object:

public Company : IAuditable {
    // Other stuff removed for bravety
    IAuditLog IAuditable.CreateEntry() {
        var entry = new CompanyAudit();
        this.auditLogs.Add(entry);
        return entry;
    }
    public virtual IEnumerable<CompanyAudit> AuditLogs {
        get { return this.auditLogs }
    }
}

The AuditLogs collection is mapped with cascading:

public class CompanyMap : ClassMap<Company> {
    public CompanyMap() {
        // Id and others removed fro bravety
        HasMany(x => x.AuditLogs).AsSet()
            .LazyLoad()
            .Access.ReadOnlyPropertyThroughCamelCaseField()
            .Cascade.All();
    }
}

And the listener just asks the auditable object to create log entries so it can update them:

internal class AuditEventListener : IPreInsertEventListener, IPreUpdateEventListener {
    public bool OnPreUpdate(PreUpdateEvent ev) {
        var audit = ev.Entity as IAuditable;
        if (audit == null)
            return false;
        Log(audit);
        return false;
    }


    public bool OnPreInsert(PreInsertEvent ev) {
        var audit = ev.Entity as IAuditable;
        if (audit == null)
            return false;

        Log(audit);
        return false;
    }
    private static void Log(IAuditable auditable) {
        var entry = auditable.CreateAuditEntry();  // Doing this for every auditable property
        entry.CreatedAt = DateTime.Now;
        entry.Who = GetCurrentUser(); // Might potentially execute a query as it links current user with log entry
        // Also other information is set for entry here
    }
}

The problem with it though is that it throws TransientObjectException when commiting the transaction:

NHibernate.TransientObjectException : object references an unsaved transient instance - save the transient instance before flushing. Type: CompanyAudit, Entity: CompanyAudit
    at NHibernate.Engine.ForeignKeys.GetEntityIdentifierIfNotUnsaved(String entityName, Object entity, ISessionImplementor session)
    at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session)
    at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session)
    at NHibernate.Persister.Collection.AbstractCollectionPersister.WriteElement(IDbCommand st, Object elt, Int32 i, ISessionImplementor session)
    at NHibernate.Persister.Collection.AbstractCollectionPersister.PerformInsert(Object ownerId, IPersistentCollection collection, IExpectation expectation, Object entry, Int32 index, Boolean useBatch, Boolean callable, ISessionImplementor session)
    at NHibernate.Persister.Collection.AbstractCollectionPersister.Recreate(IPersistentCollection collection, Object id, ISessionImplementor session)
    at NHibernate.Action.CollectionRecreateAction.Execute()
    at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
    at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
    at NHibernate.Engine.ActionQueue.ExecuteActions()
    at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
    at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
    at NHibernate.Impl.SessionImpl.Flush()
    at NHibernate.Transaction.AdoTransaction.Commit()

As the cascading is set to All I expected NH to handle this. I also tried to modify the collection using state but pretty much the same happens.

So the question is what is the last chance to modify object's associations before it gets saved?

Thanks,
Dmitriy.

+1  A: 

http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx

The short of it seems to be that by the firing of OnPreInsert, NHibernate has already determined what needs to be updated. If something becomes dirty after that, you have to further update the "entity state", the entry for the object in the list of "dirty" objects.

Your implementation of IAuditable on Company makes a change to that object; namely, adding a new object to a collection. In the case of creating entire new objects, the best practice (as mentioned by Revin Hart in the comments of the blog post) seems to be to create a child Session and save the new object there. That sounds much easier than adding all the necessary entries to the entity state with Set() calls. Try grabbing the IAuditLog from your IAuditable.CreateEntry() call, and saving it using code similar to the following:

public bool OnPreInsert(PreInsertEvent ev) {
    var audit = ev.Entity as IAuditable;
    if (audit == null)
        return false;

    var log = Log(audit);

    ISession newSession = ev.Source.PersistenceContext.Session.GetSession();
    newSession.Save(log);
    return false;
}


private static IAuditLog Log(IAuditable auditable) {
    var entry = auditable.CreateAuditEntry();  // Doing this for every auditable property
    entry.CreatedAt = DateTime.Now;
    entry.Who = GetCurrentUser(); // Might potentially execute a query as it links current user with log entry

    return entry;
}
KeithS
I tried that with no success. My solution at was to use the old-style Interceptor. With it I could easily implement `OnFlushDirty` and `OnSave`.
Dmytrii Nagirniak
I think @KeithS is correct, that you need to save the audit object inside the event. I've run into some issues with that solution though, with the returned child session being marked as closed. Checking for this condition and using the main session when that is that case seems to fix it (and also seems very wrong) so I'm still looking for a better solution to this problem...
Kendrick
This is another link to Ayende's blog that might be helpful: http://ayende.com/Blog/archive/2009/08/30/avoid-soft-deletes.aspx
Kendrick