title: NHibernate not persisting many-to-many relationship
body: <p>I'm currently using NHibernate as my data access layer, using Fluent NHibernate to create the mapping files for me. I have two classes, TripItem and TripItemAttributeValue, which have a many-to-many relation between them.</p>
  
  <p>The mapping is as follows:</p>
  
  <pre><code>public class TripItemMap : ClassMap&lt;TripItem2&gt;
  {
      public TripItemMap()
      {
          WithTable("TripItemsInt");
          NotLazyLoaded();
  
          Id(x =&gt; x.ID).GeneratedBy.Identity().WithUnsavedValue(0);
          Map(x =&gt; x.CreateDate, "CreatedOn").CanNotBeNull();
          Map(x =&gt; x.ModifyDate, "LastModified").CanNotBeNull();
  
          /* snip */
  
          HasManyToMany&lt;TripItemAttributeValue&gt;(x =&gt; x.Attributes).AsBag()
              .WithTableName("TripItems_TripItemAttributeValues_Link")
              .WithParentKeyColumn("TripItemId")
              .WithChildKeyColumn("TripItemAttributeValueId")
              .LazyLoad();
      }
  }
  
  public class TripItemAttributeValueMap : ClassMap&lt;TripItemAttributeValue&gt;
  {
      public TripItemAttributeValueMap()
      {
          WithTable("TripItemAttributeValues");
  
          Id(x =&gt; x.Id).GeneratedBy.Identity();
          Map(x =&gt; x.Name).CanNotBeNull();
  
          HasManyToMany&lt;TripItem2&gt;(x =&gt; x.TripItems).AsBag()
              .WithTableName("TripItems_TripItemAttributeValues_Link")
              .WithParentKeyColumn("TripItemAttributeValueId")
              .WithChildKeyColumn("TripItemId")
              .LazyLoad();
      }
  }
  </code></pre>
  
  <p>At some point in my application I fetch existing attributes from the database, add them to tripItem.Attributes, then save the tripItem object. In the end, the TripItems_TripItemAttributeValues_Link never gets any new records, resulting in the relations not being persisted.</p>
  
  <p>If it helps, these are the mapping files generated by Fluent NHibernate for these classes:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="ETP.Core" namespace="ETP.Core.Domain"&gt;
    &lt;class name="TripItem2" table="TripItemsInt" xmlns="urn:nhibernate-mapping-2.2" lazy="false"&gt;
      &lt;id name="ID" column="ID" type="Int32" unsaved-value="0"&gt;
        &lt;generator class="identity" /&gt;
      &lt;/id&gt;
      &lt;property name="CreateDate" column="CreatedOn" type="DateTime" not-null="true"&gt;
        &lt;column name="CreatedOn" /&gt;
      &lt;/property&gt;
      &lt;property name="ModifyDate" column="LastModified" type="DateTime" not-null="true"&gt;
        &lt;column name="LastModified" /&gt;
      &lt;/property&gt;
      &lt;bag name="Attributes" lazy="true" table="TripItems_TripItemAttributeValues_Link"&gt;
        &lt;key column="TripItemId" /&gt;
        &lt;many-to-many column="TripItemAttributeValueId" class="ETP.Core.Domain.TripItemAttributeValue, ETP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /&gt;
      &lt;/bag&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="ETP.Core" namespace="ETP.Core.Domain"&gt;
    &lt;class name="TripItemAttributeValue" table="TripItemAttributeValues" xmlns="urn:nhibernate-mapping-2.2"&gt;
      &lt;id name="Id" column="Id" type="Int32"&gt;
        &lt;generator class="identity" /&gt;
      &lt;/id&gt;
      &lt;property name="Name" column="Name" length="100" type="String" not-null="true"&gt;
        &lt;column name="Name" /&gt;
      &lt;/property&gt;
      &lt;bag name="TripItems" lazy="true" table="TripItems_TripItemAttributeValues_Link"&gt;
        &lt;key column="TripItemAttributeValueId" /&gt;
        &lt;many-to-many column="TripItemId" class="ETP.Core.Domain.TripItem2, ETP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /&gt;
      &lt;/bag&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>What am I doing wrong here ?</p>