title: Storing an ordered child collection in NHibernate
body: <p>I'm having trouble getting my head around the way I should implement an ordered child relationship with NH.</p>
  
  <p>In the code world, I have:</p>
  
  <pre><code>class Parent
  {
      public Guid Id;
      public IList&lt;Child&gt; Children;
  }
  
  class Child
  {
      public Guid Id;
      public Parent Parent;
  }
  </code></pre>
  
  <p>A <code>Parent</code> has a list of <code>Child[ren]</code> with an order. In reality, the <code>Children</code> collection will contain unique <code>Child</code>s which will be enforced by other code (i.e. it will never be possible to add the same child to the collection twice - so i dont <em>really care</em> if the NH collection enforces this)</p>
  
  <p>How should I implement the mappings for both classes?</p>
  
  <p>From my understanding:</p>
  
  <ul>
  <li><code>Bags</code> have no order, so i dont want this</li>
  <li><code>Sets</code> have no order, but i could use <code>order-by</code> to do some sql ordering, but what do i order by? I can't rely on a sequential ID. so i dont want this?</li>
  <li><code>Lists</code> are a duplicate-free collection, where the unique-key is the <code>PK</code> and the <code>index</code> column, so i do want this?</li>
  </ul>
  
  <p>So, using a <code>list</code>, i have the following:</p>
  
  <pre><code>&lt;list cascade="all-delete-orphan" inverse="true" name="Children"&gt;
      &lt;key&gt;
          &lt;column name="Parent_id" /&gt; 
      &lt;/key&gt;
      &lt;index&gt;
          &lt;column name="SortOrder" /&gt; 
      &lt;/index&gt;
      &lt;one-to-many class="Child" /&gt; 
  &lt;/list&gt;
  </code></pre>
  
  <p>When I insert a parent which a child on it, i see the following SQL:</p>
  
  <pre><code>Insert into Child (id, Parent_id) values (@p0, @p1)
  </code></pre>
  
  <p>I.e, why doesn't it insert the SortOrder?</p>
  
  <p>If I do a <code>SchemaExport</code> the SortOrder column is created on the Child table.</p>
  
  <p>:(</p>
  
  <p>If I set <code>Inverse="false"</code> on the relationship, i see the same SQL as above, followed by:</p>
  
  <pre><code>UPDATE "Child" SET Parent_id = @p0, SortOrder = @p1 WHERE Id = @p2
  </code></pre>
  
  <p>Why does it still <code>INSERT</code> the Parent_id with <code>inverse="false"</code> and why doesn't it insert the SortOrder with <code>inverse="true"</code>?</p>
  
  <p>Am I approaching this totally wrong?</p>
  
  <p>Is it also true that assuming this was working, if I were to do:</p>
  
  <pre><code>parentInstance.Children.Remove(parentInstance.Children[0]);
  </code></pre>
  
  <p>save the parent and reload it, that the <code>list</code> would have a null in position 0, instead of shuffling the rest up?</p>
  
  <p>Thanks</p>