title: Fluent NHibernate Returning Duplicates with Many to Many Mappings
body: <p>I am using NHibernate with Fluent mapping and having an issue with duplicate entries when I join against a many-to-many relationship.  My simple example below has two classes, PurchaseOrder and Product.  A PurchaseOrder can have many Products and a Product can be part of many PurchaseOrders.</p>
  
  <p>When I attempt to retrieve a PurchaseOrder and it's Products, I get the same PurchaseOrder repeated for each Product.  (So if a PurchaseOrder has 5 Products I will see the same PurchaseOrder in my result 5 times.  Each with all 5 Products.)</p>
  
  <p>Here is my setup:</p>
  
  <h2>Tables</h2>
  
  <pre><code>PurchaseOrder
      OrderID  OrderDate
      1        2013-01-01
      2        2013-01-02
  
  Product
      ProductID   Name
      1           Widget
      2           Thing
  
  OrderProducts
      OrderID ProductID
      1       1
      1       2
      2       1
      2       2
  </code></pre>
  
  <h2>Classes</h2>
  
  <pre><code>public class PurchaseOrder
  {
      public virtual int OrderID { get; set; }
      public virtual DateTime? OrderDate { get; set; }
      public virtual IList&lt;Product&gt; Products { get; set; }
  }
  
  public class Product
  {
      public virtual int ProductID { get; set; }
      public virtual string Name { get; set;  }
      public virtual IList&lt;PurchaseOrder&gt; Orders { get; set; }
  }
  </code></pre>
  
  <h2>Mappings</h2>
  
  <pre><code>public class PurchaseOrderMapping : ClassMap&lt;PurchaseOrder&gt;
  {
      public PurchaseOrderMapping()
      {
          Id(x =&gt; x.OrderID, "OrderID");
          Map(x =&gt; x.OrderDate, "OrderDate");
  
          HasManyToMany(x =&gt; x.Products)
              .Table("OrderProducts")
              .Schema("dbo")
              .ParentKeyColumn("OrderID")
              .ChildKeyColumn("ProductID");
  
          Schema("dbo");
          Table("PurchaseOrder");
      }
  }
  
  public class ProductMapping : ClassMap&lt;Product&gt;
  {
      public ProductMapping ()
      {
          Id(x =&gt; x.ProductID, "ProductID");
          Map(x =&gt; x.Name, "Name");
  
          HasManyToMany(x =&gt; x.Orders)
              .Table("OrderProducts")
              .Schema("dbo")
              .ParentKeyColumn("OrderID")
              .ChildKeyColumn("ProductID")
              .Inverse();
  
          Schema("dbo");
          Table("Product");
      }
  }
  </code></pre>
  
  <h2>QueryOver</h2>
  
  <pre><code>var orderList = session.QueryOver&lt;PurchaseOrder&gt;()
                 .JoinQueryOver&lt;Product&gt;(o =&gt; o.Products)
                 .List();
  </code></pre>
  
  <p>I expect orderList to have 2 PurchaseOrders, <strong>but it actually has 4</strong>.  The object corresponding to OrderID=1 is repeated, as is OrderID=2</p>
  
  <pre><code>foreach(var o in orderList) { Console.WriteLine(o.OrderID); }
  
  Output:
  1
  1
  2
  2
  </code></pre>
  
  <p>To take it further, if I compare the objects with the same ID, they are the same object.</p>
  
  <pre><code>System.Console.WriteLine(Object.ReferenceEquals(orderList[0], orderList[1]));
  System.Console.WriteLine(Object.ReferenceEquals(orderList[2], orderList[3]));
  
  Output:
  True
  True
  </code></pre>
  
  <p><strong>Why is NHibernate duplicating the objects in the result?  How do I exclude them and get just my list of 2 Orders, with their corresponding 2 Products each?</strong></p>