title: Mapping lists of subclasses
body: <p>I have an NHibernate problem with lists, which are mapped as subclasses of an abstract class.</p>
  
  <p>First here is the mapping for the abstract class:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                     auto-import="false"
                     assembly="Magma.Core"
                     namespace="Magma.Core.Business"&gt;
      &lt;class name="SyndicatePart" table="biz_syndicatepart" abstract="true" lazy="false"&gt;
          &lt;id name="Id" column="id"&gt;
              &lt;generator class="guid.comb" /&gt;
          &lt;/id&gt;
          &lt;discriminator column="parttype" not-null="true" /&gt;
  
          &lt;property name="Identifier" column="name" not-null="true" /&gt;
          &lt;property name="Share" column="share" not-null="true" /&gt;
          &lt;property name="CadasterNumber" column="cadaster_number" not-null="true" /&gt;
  
          &lt;many-to-one name="Account" column="accountid" lazy="proxy" cascade="all" /&gt;
          &lt;many-to-one name="Syndicate" column="syndicateid" lazy="proxy" cascade="all" /&gt;
  
          &lt;subclass name="Condo" discriminator-value="condo" lazy="false"&gt;
              &lt;property name="OwnerType" column="ownertype" /&gt;
  
                  &lt;many-to-one name="Building" column="buildingid" /&gt;
              &lt;many-to-one name="Address" column="addressid" /&gt;
  
              &lt;bag name="Tenants" access="field.camelcase-underscore" table="biz_tenant" inverse="true" cascade="all-delete-orphan"&gt;
                  &lt;key column="syndicatepartid" /&gt;
                  &lt;one-to-many class="Tenant" /&gt;
              &lt;/bag&gt;
          &lt;/subclass&gt;
  
          &lt;subclass name="Parking" discriminator-value="park" lazy="false" /&gt;
          &lt;subclass name="Locker" discriminator-value="lock" lazy="false" /&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Notice the subclasses "Condo", "Parking" and "Locker" (in my case, only Condo has additional properties). And this is the mapping of the object using lists of these subclasses:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                     assembly="Magma.Core"
                     namespace="Magma.Core.Business"&gt;
      &lt;class name="Syndicate" table="biz_syndicate" abstract="true" lazy="false"&gt;
          &lt;id name="Id" column="id"&gt;
              &lt;generator class="guid.comb" /&gt;
          &lt;/id&gt;
          &lt;discriminator column="orientation" not-null="true" /&gt;
  
          &lt;property name="Name" column="name" not-null="true" /&gt;
  
          &lt;many-to-one name="Manager" column="managerid" cascade="all-delete-orphan" /&gt;
  
          &lt;bag name="Buildings" table="biz_building" inverse="true" cascade="all-delete-orphan"&gt;
              &lt;key column="syndicateid" /&gt;
              &lt;one-to-many class="Building" /&gt;
          &lt;/bag&gt;
  
          &lt;bag name="Parkings" table="biz_syndicatepart" inverse="true" cascade="all-delete-orphan"&gt;
              &lt;key column="syndicateid" /&gt;
              &lt;one-to-many class="Parking" /&gt;
          &lt;/bag&gt;
  
          &lt;bag name="Lockers" table="biz_syndicatepart" inverse="true" cascade="all-delete-orphan"&gt;
              &lt;key column="syndicateid" /&gt;
              &lt;one-to-many class="Locker" /&gt;
              &lt;/bag&gt;
  
          &lt;subclass name="VerticalSyndicate" discriminator-value="vertical" lazy="false" /&gt;
          &lt;subclass name="HorizontalSyndicate" discriminator-value="horizontal" lazy="false" /&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Each of the lists is mapped as a bag pointing to the same table but of different class depending on the list (Condo, Parking and Locker).</p>
  
  <p>Now to the problem. The problem is that when I try to access any of those lists, NHibernate fetches <em>all</em> of the rows in the <code>biz_syndicatepart</code> table and casting it to the proper class depending on the list. So let's say I have 3 rows in the table, if I access the <code>Parkings</code> list, I'll have 3 parkings. If I access the <code>Lockers</code> list, I'll have 3 lockers! Here is the SQL generated for the parkings list:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT parkings0_.syndicateid     as syndicat7_1_,
         parkings0_.id              as id1_,
         parkings0_.id              as id39_0_,
         parkings0_.name            as name39_0_,
         parkings0_.share           as share39_0_,
         parkings0_.cadaster_number as cadaster5_39_0_,
         parkings0_.accountid       as accountid39_0_,
         parkings0_.syndicateid     as syndicat7_39_0_
  FROM   biz_syndicatepart parkings0_
  WHERE  parkings0_.syndicateid = '2310fcdf-8ab3-48dd-9a75-9f1e00f6f4fd' /* @p0 */
  </code></pre>
  
  <p>First thing, notice the double <code>parkings0_.id</code>. Is this normal? Same thing for the <code>parkings0_.syndicateid</code> (first and last row of the select statement). This I really don't understand. </p>
  
  <p>Also, Notice that <em>no</em> discriminating <code>WHERE</code> clause is inserted to specify which type of list I want. I would assume that if I would access the <code>Parkings</code> list I would see a <code>WHERE [discriminator-column] = [discriminator-value]</code>, in my case <code>WHERE parttype = 'park'</code> but it's not in the statement so this is why every row gets returned.</p>
  
  <p>I read that this might be a bug in NHibernate (I'm currently using version 3.1 GA) but reading the description of the bug it seems that it occurs when the key of the list is in the subclass table when using the table per subclass strategy (joined-subclass) so I don't think it applies to my situation.</p>
  
  <p>Can someone help me with this?! Do I have a problem with my mapping files? Why the double <code>_id</code> in the SELECT and why no discriminator <code>WHERE</code> clause?</p>