title: Joining to a subclass of a Table per Class hierarchy in NHibernate
body: <p>I'm using the table per subclass mapping inheritance in NHibernate.  I have a parent <code>Attribute</code> table and a child <code>AccountAttribute</code> table.  The child <code>AccountAttribute</code> table has another foreign key in it to a <code>CustomerProfile</code> table.  The <code>CustomerProfile</code> table has a zero or more relationship to the <code>AccountAttribute</code> table; meaning I'll have a collection of <code>AccountAttributes</code> in my <code>CustomerProfile</code> class.  </p>
  
  <p>How do I map the <code>CustomerProfile</code> table to the <code>AccountAttribute</code> table in my NHibernate mapping so that the <code>CustomerProfile</code> class is hydrated with it's correct <code>AccountAttributes</code>?</p>
  
  <p><strong>Tables</strong></p>
  
  <p><strong>Attribute:</strong> Attribute_Id (PK)</p>
  
  <p><strong>AccountAttribute:</strong>
  AccountAttribute_Id (PK); 
  Attribute_Id (FK); 
  CustomerProfile_Id (FK)</p>
  
  <p><strong>CustomerProfile:</strong>
  CustomerProfile_Id (PK)  </p>
  
  <p>Mapping for the Attribute/AccountAttribute hierarchy.</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="Attribute, CustomerProfile" lazy="false"&gt;
  
      &lt;id name="Identifier" column="Attribute_Id" access="field.camelcase"&gt;
        &lt;generator class="identity"/&gt;
      &lt;/id&gt;
  
      &lt;joined-subclass name="AccountAttribute, CustomerProfile" table="AccountAttribute" lazy="false"&gt;
        &lt;key column="Attribute_Id" /&gt;
        &lt;property name="ValueText" column="Value_Txt" access="nosetter.camelcase" /&gt;
      &lt;/joined-subclass&gt;
  
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Mapping for the Account object</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="Account, CustomerProfile" lazy="false"&gt;
  
      &lt;id name="Identifier" column="Account_Id" access="field.camelcase"&gt;
        &lt;generator class="identity"/&gt;
      &lt;/id&gt;
  
      &lt;!-- How do I map to the AccountAttributes table here to get the correct set? --&gt;
      &lt;bag name="AccountAttributes" access="field.camelcase" table="AccountAttribute"&gt;
        &lt;key column="Account_Id" /&gt;
        &lt;one-to-many class="AccountAttribute, CustomerProfile"/&gt;
      &lt;/bag&gt;
  
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Thanks,</p>
  
  <p>Kyle</p>