tags:

views:

73

answers:

2

I'm using nhibernate and it appears that changes to my new object are not persisted. After creating and saving an object I then modify it and commit the transaction. However none of the modifications are saved. The strange thing is this code was working previously and I have no idea what could cause this. Nothing was changed that's obviously related..

As an attempted work around I saved the object later in the procedure after all the changes where made however I was greeted with an Assertion Failure collection [] was not processed by flush.

Any ideas?

A: 

I have no idea what happened but the issue has been resolved, as far as I can tell the initial result shouldn't have happened. However the assertion failure was a very real problem, I was loading a user in the pre insert event listener for auditing reasons and users contain collections which were obviously not processed.

Lavinski
You may be doing this already but you should be using a new session in your event listeners.
ShaneC
A: 

I've found that using the Interceptor instead of the supposedly newer Event Listeners provides the correct functionality.

/// <summary> Updates auditable objects </summary>
public class AuditEventListener : EmptyInterceptor, IPreInsertEventListener, IPreUpdateEventListener
{
    private ISecurityManager securityManager;

    public override bool OnFlushDirty( object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types )
    {
        var auditable = entity as IAuditable;
        if (auditable != null) {

            Set( x => auditable.Modifier, propertyNames, auditable, currentState, SecurityManager.Identity );
            //Set( x => auditable.DateModified, args.Persister, auditable, args.State, TwentyClock.Now );

            return true;
        }
        return false;
    }

    public bool OnPreInsert( PreInsertEvent args )
    {
        var auditable = args.Entity as IAuditable;
        if (auditable != null) {

            Set( x => auditable.Creator, args.Persister.PropertyNames, auditable, args.State, SecurityManager.Identity );
            Set( x => auditable.DateAdded, args.Persister.PropertyNames, auditable, args.State, TwentyClock.Now );
        }
        return false;
    }
// Hidden methods etc..
}
Lavinski