Note that there are some explanatory texts on larger screens.

plurals
  1. POnhibernate cascade - problem with detached entities
    primarykey
    data
    text
    <p>I am going nuts here trying to resolve a cascading update/delete issue :-)</p> <p>I have a Parent Entity with a collection Child Entities. If I modify the list of Child entities in a detached Parent object, adding, deleting etc - I am not seeing the updates cascaded correctly to the Child collection.</p> <p><strong>Mapping Files:</strong></p> <pre><code> &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Domain" namespace="Domain"&gt; &lt;class name="Parent" table="Parent" &gt; &lt;id name="Id"&gt; &lt;generator class="guid.comb" /&gt; &lt;/id&gt; &lt;version name="LastModified" unsaved-value="0" column="LastModified" /&gt; &lt;property name="Name" type="String" length="250" /&gt; &lt;bag name="ParentChildren" lazy="false" table="Parent_Children" cascade="all-delete-orphan" inverse="true"&gt; &lt;key column="ParentId" on-delete="cascade" /&gt; &lt;one-to-many class="ParentChildren" /&gt; &lt;/bag&gt; &lt;/class&gt; &lt;class name="ParentChildren" table="Parent_Children"&gt; &lt;id name="Id"&gt; &lt;generator class="guid.comb" /&gt; &lt;/id&gt; &lt;version name="LastModified" unsaved-value="0" column="LastModified" /&gt; &lt;many-to-one name="Parent" class="Parent" column="ParentId" lazy="false" not-null="true" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p><strong>Test</strong></p> <pre><code> [Test] public void Test() { Guid id; int lastModified; // add a child into 1st session then detach using(ISession session = Store.Local.Get&lt;ISessionFactory&gt;("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&lt;Child&gt;() 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&lt;ISessionFactory&gt;("SessionFactory").OpenSession()) { Parent parent = new Parent("Test"); parent.Id = id; parent.LastModified = lastModified; session.Update(parent); session.Flush(); } } </code></pre> <p>I assume that the fact that the product has been updated to have no children in its collection - the children would be deleted in the Parent_Child table. The problems seems to be something to do with attaching the Product to the new session? As the cascade is set to all-delete-orphan I assume that changes to the collection would be propagated to the relevant entities/tables? In this case deletes?</p> <p>What am I missing here?</p> <p>C</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload