title: Removing an item from a collection(NHibernate)
body: <p>I have parent child relationship between two entities(Parent and Child).</p>
  
  <p>My Parent mapping is as follows:</p>
  
  <pre><code>&lt;class name="Parent" table="Parents"&gt;
      ...
      &lt;bag name="Children" cascade="all"&gt;
          &lt;key column="ParentID"&gt;&lt;/key&gt;
          &lt;one-to-many class="Child"&gt;&lt;/one-to-many&gt;
      &lt;/bag&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>I would like to execute the following:</p>
  
  <pre><code>someParent.Children.Remove(someChild);
  </code></pre>
  
  <p>The Child class has a reference to another parent class, Type. The relationship looks like </p>
  
  <p><img src="https://yuml.me/diagram/scruffy/class/%5BParent%5D+1-%3E%5C*%5BChild%5D%2C%20%5BType%5D+1-%3E*%5BChild%5D" alt="relation diagram"></p>
  
  <p><em>Note: I apologize for the non-linked url above, I couldn't seem to get past the asterisk in the url string using Markup</em></p>
  
  <p>Due to this relationship, when the above code is called, instead of a DELETE query, an UPDATE query is generated which removes the ParentID from the Child table(sets to null).</p>
  
  <p>Is it possible to force NHibernate to delete the child record completely, when removed from the Parent.Children collection?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>@Spencer's Solution</p>
  
  <p>Very attractive solution as this is something that can be implemented in future classes. However, due to the way sessions are handled(in my particular case) in the repository pattern, this is near impossible as we would have to pass session types(CallSessionContext/WebSessionContext) depending on the application.</p>
  
  <p>@Jamie's Solution</p>
  
  <p>Simple and quick to implement, however I've hit another road block. My child entity looks as follows:
  <img src="https://yuml.me/diagram/class/%5BChild%7CChildID%3BParentID%3BTypeID%3BName%3B%5D" alt="entity object"></p>
  
  <p>When using the new method, NHibernate generates an update statement setting the TypeID and ParentID to null, as opposed to a single delete outright. If I missed something within the implementation, let me know as this method would be painless to move forward with.</p>
  
  <p>@<a href="http://knol.google.com/k/fabio-maulo/nhibernate-chapter-16/1nr4enxv3dpeq/19#16(2E)5(2E)(C2)(A0)Understanding_Collection_performance" rel="nofollow noreferrer">The One-Shot-Delete solution described here</a>, outlines an idea dereferencing the collection to force a single delete. Same results as above however, an update statement is issued.</p>
  
  <pre><code>//Instantiate new collection and add persisted items
  List&lt;Child&gt; children = new List&lt;Child&gt;();
  children.AddRange(parent.Children);
  
  //Find and remove requested items from new collection
  var childrenToRemove = children
      .Where(c =&gt; c.Type.TypeID == 1)
      .ToList();
  
  foreach (var c in childrenToRemove) { children.Remove(m); }
  parent.Children = null;
  
  //Set persisted collection to new list
  parent.Children = Children;
  </code></pre>
  
  <p><strong>Solution</strong></p>
  
  <p>Took a bit of digging, but Jamie's solution came through with some additional modifications. For future readers, based on my class model above:</p>
  
  <p>Type mapping - Inverse = true, Cascade = all</p>
  
  <p>Parent mapping - Inverse = true, Cascade = all-delete-orphan</p>
  
  <p>Remove methods as described in Jamie's solution works. This does produce a single delete statement per orphaned item, so there is the possibility for tuning in the future, however the end result is successful.</p>