title: Fluent NHibernate - HasManyToMany NHibernate.MappingException: Repeated column in mapping for collection
body: <p>I'm a NHibernate novice trying to configure an existing database with Fluent NHibernate. The problem is with a many-to-many mapping, in this example represented by libraries and books. I guess this should be really basic stuff, but I get the following exception:</p>
  
  <pre><code>FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
  
   ---&gt; NHibernate.MappingException: Repeated column in mapping for collection: MvcNhibernatePoc.Models.Book.Libraries column: BookId
  </code></pre>
  
  <p>The structure of the database should not be changed and looks something like:</p>
  
  <pre><code>Table **Book**
  BookId (int)
  BookName (varchar(255))
  
  Table **Library**
  LibraryId (int)
  LibraryName (varchar(255))
  
  Table **Book_Library**
  Id (int)
  BookId (int)
  LibraryId (int)
  </code></pre>
  
  <p>Based on this I have created the following domain classes:</p>
  
  <pre><code>public class Library
      {
          public virtual int LibraryId { get; set; }
          public virtual string Name { get; set; }
  
          public virtual IList&lt;Book&gt; Books { get; set; }
  
          public Library()
          {
              Books = new List&lt;Book&gt;();
          }
      }
  
  
  public class Book
  {
      public virtual int BookId { get; set; }
      public virtual string Name { get; set; }
  
      public virtual IList&lt;Library&gt; Libraries { get; set; }
  
      public Book()
      {
          Libraries = new List&lt;Library&gt;();
      }
  }
  </code></pre>
  
  <p>Mapping:</p>
  
  <pre><code>public class LibraryMap : ClassMap&lt;Library&gt;
  {
      public LibraryMap()
      {
          Table("Library");
          Id(l =&gt; l.LibraryId).Column("LibraryId");
          Map(l =&gt; l.Name).Column("LibraryName");
  
          HasManyToMany&lt;Book&gt;(l =&gt; l.Books)
              .Table("Book_Library")
              .ParentKeyColumn("LibraryId")
              .ChildKeyColumn("LibraryId")
              .Cascade.SaveUpdate();
      }
  }
  
  public class BookMap : ClassMap&lt;Book&gt;
  {
      public BookMap()
      {
          Table("Book");
          Id(b =&gt; b.BookId).Column("BookId");
          Map(b =&gt; b.Name).Column("BookName");
  
          HasManyToMany&lt;Library&gt;(b =&gt; b.Libraries)
              .Table("Book_Library")
              .ParentKeyColumn("BookId")
              .ChildKeyColumn("BookId")
              .Cascade.SaveUpdate()
              .Inverse();
      }
  }
  </code></pre>
  
  <p>Fluent configuration:</p>
  
  <pre><code>Fluently.Configure()
              .Database(MsSqlConfiguration.MsSql2008.ConnectionString(conString).ShowSql)
              .Mappings(m =&gt; m.FluentMappings
              .AddFromAssemblyOf&lt;Library&gt;())
              .BuildSessionFactory();
  </code></pre>
  
  <p>And finally my failing testcode:</p>
  
  <pre><code>var library = new Library { LibraryId = 1, Name = "Alexandria library"};
  var book = new Book { BookId = 1, Name = "Pyramids for dummies" };
  
  library.Books.Add(book);
  book.Libraries.Add(library);
  
  using (var session = NHibernateHelper.OpenSession())
  {
      using (var transaction = session.BeginTransaction())
      {
          session.Save(library);
          session.Flush();
  
          transaction.Commit();
  
          Console.WriteLine("Saved library " + library.Name);
      }
  }
  </code></pre>