title: Preserve orphans, NHibernate one-to-many
body: <p>I'm successfully using a one-to-many relationship in NHibernate apart from one thing. When I remove a child record from the collection held in the parent, I don't want it to delete the child record. I just want it to blank out the foreign key in the child record.</p>
  
  <p>This is so that I can reattach the child record to a different parent later on. </p>
  
  <p>Is this possible? I have tried various cascade options but they all seem to delete the child when I call Remove() on the collection.</p>
  
  <p>Here's my parent mapping (a 'SectionItem')</p>
  
  <pre><code>  &lt;class name="Munch.Domain.MenuSection, Munch.Dao" table="menu_section"&gt;
      &lt;id name="Id" type="Int32"&gt;
        &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;property name="Position" /&gt;
      &lt;property name="Title" /&gt;
      &lt;property name="Description" /&gt;
      &lt;bag name="MenuItems" cascade="save-update"&gt;
        &lt;key column="menuSectionId"/&gt;
        &lt;one-to-many class="MenuItem"/&gt;
      &lt;/bag&gt;    
    &lt;/class&gt;
  </code></pre>
  
  <p>Child object (a 'MenuItem')</p>
  
  <pre><code>  &lt;class name="Munch.Domain.MenuItem, Munch.Dao" table="menu_item"&gt;
      &lt;id name="Id" type="Int32"&gt;
        &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;property name="Title" /&gt;
      &lt;property name="Description" /&gt;
      &lt;property name="Type" /&gt;
      &lt;property name="Image" /&gt;
    &lt;/class&gt;
  </code></pre>
  
  <p>And here is a test that builds up the child collection and saves the parent/children in one go. Then I remove one child (making a note of it's id before I do) and then I try to retrieve the 'removed' child to check that it is still in the database. I would expect it to be there, but with a null foreign key back to the parent.</p>
  
  <pre><code>    // Create the menu section
      MenuSection ms = new MenuSection();
      ms.Title = "MenuSectionTitle";
      ms.Description = "Description";
      ms = menuSectionDao.Save(ms);
  
      // Create a couple of menu items
      MenuItem item1 = new MenuItem();
      item1.Title = "AAA";
      item1.Description = "AAA Desc";
  
      MenuItem item2 = new MenuItem();
      item2.Title = "BBB";
      item2.Description = "BBB Desc";
  
      List&lt;MenuItem&gt; items = new List&lt;MenuItem&gt;();
      items.Add(item1);
      items.Add(item2);
  
      // Add the items to the menu section
      ms.MenuItems = items;
  
      // Save it and check
      menuSectionDao.Save(ms);
      Assert.IsNotNull(ms, "Menu Section wasn't saved");
      Assert.True(ms.Id &gt; 0, "Menu Section id is not greater than zero, probably an error");
  
      log.Debug("MenuSection saved with id " + ms.Id);
  
      // See what's been saved
      MenuSection ms2 = menuSectionDao.Find(ms.Id);
      Assert.IsNotNull(ms2, "Retrieved a null value");
  
      // Check that the menu items were saved too
      Assert.IsNotNull(ms2.MenuItems);
      Assert.IsTrue(ms2.MenuItems.Count == 2);
      Assert.AreEqual(ms2.MenuItems[0].Title, "AAA");
      Assert.AreEqual(ms2.MenuItems[1].Title, "BBB"); 
  
      // Try and remove the item
      int item1Id = ms2.MenuItems[0].Id;
  
      log.Debug("Deleting item 0 with id " + item1Id);
      ms2.MenuItems.RemoveAt(0);
      menuSectionDao.Save(ms2);
  
      MenuSection ms3 = menuSectionDao.Find(ms.Id);
      Assert.IsTrue(ms3.MenuItems.Count == 1);
  
      // Check we haven't deleted the menu item
      MenuItem item = menuItemDao.Find(item1Id);
      Assert.IsNotNull(item);
  }
  </code></pre>
  
  <p>(the test is failing on the last line by the way)</p>
  
  <p>Thanks</p>