R – Nhibernate: Uninitialized proxy passed to save

nhibernate

I've got a problem with NHibernate. The error message is:

Uninitialized proxy passed to save

[PersistentObjectException:
Uninitialized proxy passed to save().
Object:
……Domain.Model……]
NHibernate.Event.Default.DefaultSaveEventListener.ReassociateIfUninitializedProxy(Object
obj, ISessionImplementor source) +104
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent
event) +90
NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent
event) +156
NHibernate.Impl.SessionImpl.SaveOrUpdate(Object
obj) +152

So what I do is, I have an aggregate root and within that a collection of objects. When saving changes, I call save on the aggregate root only. The child class doesn't have a reference to the aggreate root.

  <bag name="Children" cascade="all-delete-orphan" where="IsDeleted = 0" lazy="true">
    <key column="abcId"/>
    <one-to-many class="abc"/>
  </bag>

Now the error appears when I collect some children to be saved from the UI like:

root = repository.GetRootById(id)

    // collect children from data list 
    abc = repository.GetAbcById(abcId) 
    abc.Name = textName.Text; 
    ...

    // no root.Children.Add(abc) is performed here, since abc already is a child of root    

    repository.Save(root); 
    ... 
    Exception occurs

Now if I access root.Children before save, it works.

Anyone?

Best Answer

Fixed it.. the root was uninitialized because within the repository .GetRootById(..) I used session.Load instead of session.Get. So the root was never loaded...

Related Topic