Nhibernate cascade - single entity issue

I'm going to use nuts trying to solve the cascading update / delete problem :-)

I have a parent entity with collection children. If I change the list of children in a separate parent by adding, removing, etc. - I don't see the updates cascading correctly in the Child collection.

File matching:

  <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Domain"
                   namespace="Domain">

  <class name="Parent" table="Parent" >

    <id name="Id">
      <generator class="guid.comb" />
    </id>

    <version name="LastModified"
                    unsaved-value="0"
                    column="LastModified"
                     />

    <property name="Name" type="String" length="250" />

    <bag name="ParentChildren" lazy="false" table="Parent_Children" cascade="all-delete-orphan" inverse="true">
      <key column="ParentId" on-delete="cascade" />
      <one-to-many class="ParentChildren" />
    </bag>

  </class>

  <class name="ParentChildren" table="Parent_Children">

    <id name="Id">
      <generator class="guid.comb" />
    </id>

    <version name="LastModified"
                    unsaved-value="0"
                    column="LastModified"
                     />

    <many-to-one
   name="Parent"
   class="Parent"
   column="ParentId"
   lazy="false"
   not-null="true"
       />
  </class>
</hibernate-mapping>

      

Test

    [Test]
    public void Test()
    {
        Guid id;
        int lastModified;
        // add a child into 1st session then detach
        using(ISession session = Store.Local.Get<ISessionFactory>("SessionFactory").OpenSession())
        {
            Console.Out.WriteLine("Selecting...");
            Parent parent =  (Parent) session.Get(typeof (Parent), new Guid("4bef7acb-bdae-4dd0-ba1e-9c7500f29d47"));

            id = parent.Id;
            lastModified = parent.LastModified + 1; // ensure the detached version used later is equal to the persisted version

            Console.Out.WriteLine("Adding Child...");
            Child child = (from c in session.Linq<Child>() select c).First();
            parent.AddChild(child, 0m);

            session.Flush();
            session.Dispose(); // not needed i know
        }

        // attach a parent, then save with no Children
        using (ISession session = Store.Local.Get<ISessionFactory>("SessionFactory").OpenSession())
        {
            Parent parent = new Parent("Test");              

            parent.Id = id; 
            parent.LastModified = lastModified; 

            session.Update(parent);
            session.Flush();
        }
    }

      

My guess is the fact that the product has been updated so that there are no children in its collection - the children will be deleted in the Parent_Child table. Problems seem to be related to the product joining a new session? Since the cascade is set to all-delete-orphan, my guess is that changes to the collection will propagate to the respective entities / tables? In this case, deletes?

What am I missing here?

FROM

+2


source to share


1 answer


I was struggling with a similar problem. Not sure if my solution will fit your problem, but try using ISession.Merge instead of ISession.Update.



+2


source







All Articles