title: Problems with updating records through Many-To-Many mappings
body: <p>I have a problem where I have a many-to-many mappings in my table structure creating headaches when trying to edit a simple record.</p>
  
  <p>Example layout of where I am having problems:</p>
  
  <pre><code>Facilities Many-to-One Locations
  Facilities One-to-Many Users
  Users Many-to-Many Locations
  Users One-to-Many PreviousPasswords
  </code></pre>
  
  <p>If I make a change to a Facilities record (Change the name field) I get the following error upon save:</p>
  
  <p>collection [Users.PreviousPasswords] was not processed by flush()</p>
  
  <p>Mapping looks like:</p>
  
  <pre><code>public FacilitiesMap()
  {
      Table("Facilities");
  
      Id(x =&gt; x.ID);
      Map(x =&gt; x.Name);
      HasMany(x =&gt; x.Users).KeyColumn("FacilitiesID").Cascade.AllDeleteOrphan().Inverse();
      HasMany(x =&gt; x.Locations).KeyColumn("FacilitiesID").Cascade.AllDeleteOrphan().Inverse();
  }
  
  public UsersMap()
  {
      Table("Users");
      Id(x =&gt; x.ID);
      Map(x =&gt; x.FirstName);
      Map(x =&gt; x.LastName);
      References(x =&gt; x.Facilities, "FacilitiesID").ForeignKey("ID");
      HasMany(x =&gt; x.PreviousPasswords).KeyColumn("UsersID").Cascade.AllDeleteOrphan().Inverse();
      HasManyToMany&lt;Locations&gt;(x =&gt; x.Locations)
           .Schema("Members")
           .Table("UsersToLocations")
           .ParentKeyColumn("UsersID")
           .ChildKeyColumn("LocationsID")
           .LazyLoad()
           .Cascade.SaveUpdate().Inverse();
  }
  
  public LocationsMap()
  {
      Table("Locations");
  
      Id(x =&gt; x.ID);
      Map(x =&gt; x.Name);
      References(x =&gt; x.Facilities, "FacilitiesID").ForeignKey("ID");
      HasMany(x =&gt; x.Patients).KeyColumn("LocationsID").Cascade.AllDeleteOrphan().Inverse();
      HasManyToMany&lt;Users&gt;(x =&gt; x.Users)
           .Schema("Members")
           .Table("UsersToLocations")
           .ParentKeyColumn("LocationsID")
           .ChildKeyColumn("UsersID")
           .Cascade.SaveUpdate();
  }
  
  public PreviousPasswordsMap()
  {
      Table("PreviousPasswords");
  
      Id(x =&gt; x.ID);
      Map(x =&gt; x.Password);
      Map(x =&gt; x.DateTime);
  
      References(x =&gt; x.Users, "UsersID").ForeignKey("ID");
  }
  </code></pre>
  
  <p>The only way I can do a successful update to the Facilities record is if I use the following function to get the record before changing and saving it:</p>
  
  <pre><code>public Facilities GetFacility(int id)
  {
      return FluentSessionManager.GetSession()
          .CreateCriteria&lt;Facilities&gt;()
          .Add(Expression.Eq("ID", id))
          .SetFetchMode("Users", FetchMode.Eager)
          .SetFetchMode("Locations", FetchMode.Eager)
          .UniqueResult&lt;Facilities&gt;();
  }
  </code></pre>
  
  <p>The problem with this method is that where there are 10,000 users it takes a long time to process this query.  Or even worse, if we have 100 location as well, then it takes around 2 minutes to get the one Facilities record to edit.</p>
  
  <p>I am sure there is some kind of issue in the Mapping.  Not sure how to fix or even where to start.  Any help with this would be greatly appreciated.
  Thanks in advance.</p>