views:

161

answers:

2

Hi I'm having a problem updating child objects in the following scenario.

The mappings are as follows:

Parent: Calendar

  <bag name="defaultCategories"  inverse="true" lazy="false" 
    cascade="all-delete-orphan">
  <key column="parentID" />
  <one-to-many class="DefaultCategory"/>
  </bag>

Child: DefaultCatergory

<class name="DefaultCategory" table="tb_calendar_default_category" lazy="false">

<id name="id" column="id">
  <generator class="hilo"/>
</id>

<many-to-one name="calendar" column="parentID" not-null="true" 
 cascade="all-delete-orphan" />

Code used to update calendar:

public Calendar Update(Calendar vo)
    {
        session = NHibernateHelper.GetCurrentSession();

        tx = session.BeginTransaction();
        using (tx)
        {

            session.Update(vo);

            tx.Commit();
        }

        return vo;
    }

The problem is that when I add or delete defaultCategories via the ui and send back the updated version of the calendar to the back end, NHibernate returns the updated calendar and all seems well. However any defaultCatergories which should have been deleted are left in the default Categories table. Thus when I refresh the ui and call for the calendar again, I can see collection has not changed.

Do you think I need to delete all defaultCatergories by parentID and then recreate? I was under the impression NHibernate took care of this for you? Any help or pointers much appreciated.

+1  A: 

I wonder if NHibernate is not recognizing the Calendar as an existing instance. Take a look at this section of the documentation and see if adding a unsaved-value attribute to your mapping helps.

Stuart Childs
Thanks for that. I just looked at it and followed the advice of using saveUpdate. However, still the same problem. That being, say I have 3 child objects, delete 2 and update. I'll still end up with 3 in the db. Hmmm back to the drawing board.
+1  A: 

Is your default flushmode OnCommit? If it is not the case then may be flushing the session before commiting will fix the problem. What you are doing should work.

Gustavo.

Tavo