tags:

views:

1005

answers:

8

Hi,

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?

A: 

When you create the base object does that object in turn either create or null the cascading objects?

sstarcher
A: 

In my event listener I'm calling lifecycle events on the object which populates the children.

jonnii
A: 

Are you loading the objects in different sessions?

David Kemp
A: 

No, all from the same session, which is why it's so confusing.

jonnii
A: 

You probably break something in you event listeners. What is the complete error message you get?

Stefan Steinegger
+1  A: 

Are you trying to work with a child object that is in a list on a root aggregate entity? If you are, you need to work with the root, traverse to the child, make the changes, and save the root, not the child.

Joe
I'm not working on that project anymore, but I think that would probably be the cause.
jonnii
+1  A: 

This happened to me too.

The fix was simple : use ISession.Get() and not ISession.Load()

mathieu
rise, rise from the dead!
Jaguar
I'm getting this error and I have no references to ISession.Load or any of it's overloads.
Rabid
A: 

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.

Rabid
Ahh stupid me I was deriving from `DefaultSaveEventListener` instead of `DefaultSaveOrUpdateEventListener`, changing the superclass made this problem go away.
Rabid