title: NHibernate mapping a component list with a unique key constraint
body: <p>I'm trying to map a list of components that contains a unique key constraint on its values. </p>
  
  <p>A 'trimmed' sample of the classes are below:</p>
  
  <pre><code>public class MyObject
  {
      public virtual int Id { get; set; }
      public virtual IList&lt;Alias&gt; Aliases { get; set; }
  }
  
  public class Alias
  {
      public Alias(string type, string value)
      {
          Type = type;
          Value = value;
      }
  
      public virtual string Type { get; protected set; }
      public virtual string Value { get; protected set; }
  }
  </code></pre>
  
  <p>And the relevant NHibernate mapping from MyObject (types have been trimmed for brevity):</p>
  
  <pre><code>  &lt;list cascade="all-delete-orphan" name="Aliases" table="MyObject_Aliases" mutable="true"&gt;
        &lt;key&gt;
          &lt;column name="MyObject_id" /&gt;
        &lt;/key&gt;
        &lt;index&gt;
          &lt;column name="`Index`" /&gt;
        &lt;/index&gt;
        &lt;composite-element class="Alias"&gt;
          &lt;property name="Type" type="System.String"&gt;
            &lt;column name="Type" unique-key="UK_Alias" /&gt;
          &lt;/property&gt;
          &lt;property name="Value" type="System.String"&gt;
            &lt;column name="Value" unique-key="UK_Alias" /&gt;
          &lt;/property&gt;
        &lt;/composite-element&gt;
      &lt;/list&gt;
  </code></pre>
  
  <p>The intention is to prevent two MyObjects from containing the same Alias.</p>
  
  <p>This works perfectly well when adding a new Alias to MyObject. However, if you were to remove an alias from the list where there are aliases after the removed alias then NHibernate tries to reorder the list which causes a key violation.</p>
  
  <p>A sample of the SQL that NHibernate is generating is:</p>
  
  <pre><code>-- statement #1, saving MyObject.Aliases
  INSERT INTO MyObject_Aliases
             (MyObject_id,
              "Index",
              Type,
              Value)
  VALUES     (101 /* @p0 */,
              0 /* @p1 */,
              'A' /* @p2 */,
              'ALIAS1' /* @p3 */)
  
  INSERT INTO MyObject_Aliases
             (MyObject_id,
              "Index",
              Type,
              Value)
  VALUES     (101 /* @p0 */,
              1 /* @p1 */,
              'A' /* @p2 */,
              'ALIAS2' /* @p3 */)
  
  -- statement #2, updating MyObject.Aliases after removing the first list item
  UPDATE MyObject_Aliases
  SET    Type = 'A' /* @p0 */,
         Value = 'ALIAS2' /* @p1 */
  WHERE  MyObject_id = 101 /* @p2 */
         AND "Index" = 0 /* @p3 */
  </code></pre>
  
  <p>Statement #2 in this example throws:</p>
  
  <pre><code>NHibernate.Exceptions.GenericADOException : could not update collection rows: [MyObject.Aliases#101][SQL: UPDATE MyObject_Aliases SET Type = ?, Value = ? WHERE MyObject_id = ? AND "Index" = ?]
    ----&gt; System.Data.SQLite.SQLiteException : Abort due to constraint violation
  columns Type, Value are not unique
  </code></pre>
  
  <p>The exception itself makes perfect sense based on what is actually happening, but how do I get this to actually work as expected?</p>