C# – NHibernate: uninitialized proxy passed to save() and cascade

cnhibernate

I keep getting an NHibernate.PersistentObjectException when calling session.Save() which is due to an uninitialized proxy passed to save(). If I fiddle with my cascade settings I can make it go away, but then child objects aren't being saved.

The only other fix I have found is by adding the following to my DefaultSaveEventListener.

    protected override bool ReassociateIfUninitializedProxy(object obj, global::NHibernate.Engine.ISessionImplementor source)
    {
        if (!NHibernateUtil.IsInitialized(obj))
            NHibernateUtil.Initialize(obj);

        return base.ReassociateIfUninitializedProxy(obj, source);
    } 

This is obviously not an ideal solution.

Any ideas?

Best Answer

There mere presence of a custom DefaultSaveEventListener subclass containing no overidden or extended behaviour is enough to trigger this exception for me, using the following configuration Xml:

<event type="save-update">
    <listener class="MyNamespace.MyCustomSaveEventListener, MyAssembly" />
</event>

I am continuing this discussion in this question.

Update:

I had mistakenly derived from DefaultSaveEventListener instead of DefaultSaveOrUpdateEventListener, changing the superclass made this problem go away for me.