title: Nhibernate one-to-many with table per subclass
body: <p>I am customizing N2CMS's database structure, and met with an issue. The two classes are listed below.</p>
  
  <pre><code>public class Customer : ContentItem
  {
    public IList&lt;License&gt; Licenses { get; set; }
  }
  
  public class License : ContentItem
  {
    public Customer Customer { get; set; }
  }
  </code></pre>
  
  <p>The nhibernate mapping are as follows.</p>
  
  <pre><code>  &lt;class name="N2.ContentItem,N2" table="n2item"&gt;
      &lt;cache usage="read-write" /&gt;
      &lt;id name="ID" column="ID" type="Int32" unsaved-value="0" access="property"&gt;
        &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;discriminator column="Type" type="String" /&gt;
    &lt;/class&gt;
  
    &lt;subclass name="My.Customer,My" extends="N2.ContentItem,N2" discriminator-value="Customer"&gt;
      &lt;join table="Customer"&gt;
        &lt;key column="ItemID" /&gt;
        &lt;bag name="Licenses" generic="true" inverse="true"&gt;
          &lt;key column="CustomerID" /&gt;
          &lt;one-to-many class="My.License,My"/&gt;
        &lt;/bag&gt;
      &lt;/join&gt;
    &lt;/subclass&gt;
  
    &lt;subclass name="My.License,My" extends="N2.ContentItem,N2" discriminator-value="License"&gt;
      &lt;join table="License" fetch="select"&gt;
        &lt;key column="ItemID" /&gt;
        &lt;many-to-one name="Customer" column="CustomerID"  class="My.Customer,My" not-null="false" /&gt;
      &lt;/join&gt;
    &lt;/subclass&gt;
  </code></pre>
  
  <p>Then, when get an instance of Customer, the customer.Licenses is always empty, but actually there are licenses in the database for the customer. When I check the nhibernate log file, I find that the SQL query is like:</p>
  
  <pre><code>SELECT   licenses0_.CustomerID     as CustomerID1_,
           licenses0_.ID             as ID1_,
           licenses0_.ID             as ID2_0_,
           licenses0_1_.CustomerID   as CustomerID7_0_,
  FROM     n2item licenses0_
           inner join License licenses0_1_
             on licenses0_.ID = licenses0_1_.ItemID
  WHERE    licenses0_.CustomerID = 12 /* @p0 */
  </code></pre>
  
  <p>It seems that nhibernate believes that the CustomerID is in the 'n2item' table. I don't know why, but to make it work, I think the SQL should be something like this.</p>
  
  <pre><code>SELECT   licenses0_.ID             as ID1_,
           licenses0_.ID             as ID2_0_,
           licenses0_1_.CustomerID   as CustomerID7_0_,
  FROM     n2item licenses0_
           inner join License licenses0_1_
             on licenses0_.ID = licenses0_1_.ItemID
  WHERE    licenses0_1_.CustomerID = 12 /* @p0 */
  </code></pre>
  
  <p>Could any one point out what's wrong with my mappings? And how can I get the correct licenses of one customer? Thanks in advance.</p>