title: How do I properly update an NHibernate Many-to-Many relationship with Transient objects? TransientObjectException
body: <p>I am getting a TransientObjectException when attempting to save an object that is part of a many-to-many association.  I somewhat understand why this is happening, but want to understand how to properly accomplish what I am trying to do.</p>
  
  <p><strong>What I am trying to do, in a nutshell:</strong></p>
  
  <p>My application has a list of users and a list of roles.  Users can be assigned to multiple roles, and roles can be assigned to multiple users.  There is a web page where an admin can perform these assignments, and the assignments can be done in both directions (e.g., the admin can pick a user, then add roles to it; OR, pick a role and add users to it).</p>
  
  <p>For example, let's say the admin clicks "Edit" on user "Alice."  The admin is presented with a list of roles available and a list of roles already assigned to Alice.  The admin then assigns a new role to Alice and clicks "Save."</p>
  
  <p>On the server, the transient User and assigned Role objects are received from the client.  If I just assign the transient Role list to the User object (e.g., <code>user.Roles = roles</code>), I can update just fine.  However, if I happen to read the User from the database before making this assignment, I get a TransientObjectException on the associated Role object.</p>
  
  <p><strong>Class definitions:</strong></p>
  
  <pre><code>public class Role
  {
      public Guid ID { get; set; }
      public virtual IList&lt;User&gt; Users { get; set; }
  }
  
  public class User
  {
      public Guid ID { get; set; }
      public virtual IList&lt;Role&gt; Roles { get; set; }
  }
  </code></pre>
  
  <p><strong>Mapping:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"&gt;
    &lt;class name="Role"&gt;
      &lt;id name="ID"&gt;
        &lt;generator class="guid"/&gt;
      &lt;/id&gt;
  
      &lt;bag name="Users" table="Role_User" lazy="false" cascade="none"&gt;
        &lt;key column="RoleID" /&gt;
        &lt;many-to-many column="UserID" class="User" /&gt;
      &lt;/bag&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  
  &lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"&gt;
    &lt;class name="User"&gt;
      &lt;id name="ID"&gt;
        &lt;generator class="guid"/&gt;
      &lt;/id&gt;
  
      &lt;bag name="Roles" table="Role_User" lazy="false" cascade="none"&gt;
        &lt;key column="UserID" /&gt;
        &lt;many-to-many column="RoleID" class="Role" /&gt;
      &lt;/bag&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><strong>Code to save (works)</strong></p>
  
  <pre><code>public void UpdateUser(User user, IList&lt;Role&gt; associatedRoles)
  {
      using (var session = _sessionFactory.OpenSession())
      {
          user.Roles = associatedRoles;
          session.Merge&lt;User&gt;(user);
      }
  }
  </code></pre>
  
  <p><strong>Code to save (fails)</strong></p>
  
  <pre><code>public void UpdateUser(User user, IList&lt;Role&gt; associatedRoles)
  {
      using (var session = _sessionFactory.OpenSession())
      {
          User originalUser = session.Get&lt;User&gt;(user.ID);
          // Code that does some audit reporting/logging
          LogDifferences(originalUser, user);
          user.Roles = associatedRoles;
          session.Merge&lt;User&gt;(user);
      }
  }
  </code></pre>
  
  <p>object is an unsaved transient instance - save the transient instance before merging</p>