Note that there are some explanatory texts on larger screens.

plurals
  1. POnhibernate and All-delete-orphan
    text
    copied!<p>i have a an entity class which has a bag of child entity class like so(copied relevant lines):</p> <pre><code>&lt;class name="Entity" table="Entities" lazy="false"&gt; &lt;id name="ID"&gt; &lt;generator class="guid"/&gt; &lt;/id&gt; &lt;bag name="SubEntities" table="SubEntities" cascade="all-delete-orphan"&gt; &lt;key column="EntityID"/&gt; &lt;one-to-many class="SubEntity"/&gt; &lt;/bag&gt; &lt;/class&gt; </code></pre> <p>Now, the mapping works well and as expected in most cases (when i delete/save it cascades), but when i try to remove some subentity(child) from the bag in the Entity(parent) class - the change does not cascade and all i see in the DB is that the subentitiy's foreign key was changed to null, and not deleted as i would like.</p> <p>I've read something about nhibernate not realizing which line it needs to delete in the database (no unique id for the row) - so i tried to use the idbag instead of the bag - but the idbag does not allow a one-to-many collection in it, i've tried something of the sort:</p> <pre><code>&lt;idbag name="SubEntities" table="SubEntities" cascade="all-delete-orphan"&gt; &lt;collection-id column="Id" type="Guid"&gt; &lt;generator class="guid"/&gt; &lt;/collection-id&gt; &lt;key column="EntityID"/&gt; &lt;one-to-many class="SubEntity"/&gt; &lt;/idbag&gt; </code></pre> <p>which of course gives the error that one-to-many isnt allowed there.</p> <p>Even when i try to use the component (which i dont want as i want the child is also an entity) - i cant use an external hbm file to define it (the subentity is a rather large class by itself) so setting the entity's propertise in the parent's hbm files isnt a good idea as well.</p> <p>Could anyone help me out with explaning whats wrong and how am i supposed to fix it? i really need the subentity to be removed!</p> <p>Thanks!</p> <hr> <p>As requested - I"m pasting my hbm files:</p> <h2>For Entity:</h2> <pre><code> &lt;?xml version="1.0" encoding="utf-8" ?&gt; - &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Com.Project.Shared.Common" namespace="Com.Project.Shared.Common.Entities"&gt; - &lt;class name="Entity" table="Entities" lazy="false"&gt; - &lt;id name="ID"&gt; &lt;generator class="guid" /&gt; &lt;/id&gt; &lt;property name="Name" /&gt; &lt;property name="Description" /&gt; &lt;property name="EndTime" /&gt; &lt;property name="StartTime" /&gt; &lt;property name="State" /&gt; &lt;property name="Stored" /&gt; &lt;property name="ClassRoomID" /&gt; &lt;property name="Score" /&gt; &lt;many-to-one name="Network" class="Network" column="NetworkID" /&gt; - &lt;bag name="Scenarios" cascade="all"&gt; &lt;key column="EntityID" /&gt; &lt;one-to-many class="EntScenario" /&gt; &lt;/bag&gt; - &lt;bag name="TimeLineEvents" order-by="TimeStamp" cascade="all"&gt; &lt;key column="EntityID" /&gt; &lt;one-to-many class="TimeLineEvent" /&gt; &lt;/bag&gt; - &lt;bag name="SubEntity" table="SubEntities" cascade="all-delete-orphan"&gt; &lt;key column="EntityID" /&gt; &lt;one-to-many class="SubEntity" /&gt; &lt;/bag&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>for SubEntity:</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8" ?&gt; - &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Com.Project.Shared.Common" namespace="Com.Project.Shared.Common.Entities"&gt; - &lt;class name="SubEntity" table="SubEntities" lazy="false"&gt; - &lt;id name="ID"&gt; &lt;generator class="guid" /&gt; &lt;/id&gt; &lt;many-to-one name="Name" class="EntName" column="NameID" /&gt; &lt;many-to-one name="Station" class="EntStation" column="StationID" /&gt; - &lt;bag name="Performances" table="EntPerformances" cascade="all"&gt; &lt;key column="SubEntityID" /&gt; - &lt;composite-element class="Performance"&gt; &lt;property name="Rank" /&gt; &lt;property name="Remark" /&gt; &lt;many-to-one name="Category" class="PerformanceCategory" column="CategoryID" index="ListIndex" /&gt; &lt;/composite-element&gt; &lt;/bag&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>The tester i use is:</p> <pre><code>Entity newEntity = _dal.GetAll&lt;Entity&gt;()[0]; ObservableCollection&lt;SubEntity&gt; subEntities = newEntity.ObservableSubEntities; subEntities .RemoveAt(1); _dal.SaveItem&lt;Entity&gt;(newEntity); </code></pre> <p>This just turned the EntityID column in subentity into Null - but doesnt delete it.</p> <p>I appericiate your help, guys.</p>
 

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