title: Nhibernate return a specific type of union subclass in a QueryOver with join
body: <p>This is my current nhibenate query</p>
  
  <pre><code>        MappedHSPItemDto itemDtoAlias = null;
          TItem            itemAlias    = default(TItem);
  
          return
              Session.QueryOver&lt;TMapItem&gt;()
                  .JoinAlias(x =&gt; x.Item, () =&gt; itemAlias, JoinType.InnerJoin)
                  .Where(x =&gt; x.HealthServiceProvider == hsp)
                  .SelectList(list =&gt; list
                                          .Select(x =&gt; x.Id).WithAlias(() =&gt; itemDtoAlias.Id)
                                          .Select(x =&gt; x.Version).WithAlias(() =&gt; itemDtoAlias.Version)
                                          .Select(x =&gt; x.HSPItemCode).WithAlias(() =&gt; itemDtoAlias.HSPItemCode)
                                          .Select(x =&gt; x.HSPItemName).WithAlias(() =&gt; itemDtoAlias.HSPItemName)
                                          .Select(x =&gt; itemAlias.Code).WithAlias(() =&gt; itemDtoAlias.CirrusItemCode)
                                          .Select(x =&gt; itemAlias.Name).WithAlias(() =&gt; itemDtoAlias.CirrusItemName)
                  )
                  .TransformUsing(Transformers.AliasToBean&lt;MappedHSPItemDto&gt;()).List&lt;MappedHSPItemDto&gt;();
  </code></pre>
  
  <p>which returns this sql query</p>
  
  <pre><code>SELECT
          this_.Id as y0_,
          this_.Version as y1_,
          this_.HSPItemCode as y2_,
          this_.HSPItemName as y3_,
          itemalias1_.Code as y4_,
          itemalias1_.Name as y5_      
      FROM
          HSPMedicineMapping this_      
      inner join
          (
              select
                  Id,
                  Version,
                  Code,
                  Name,
                  GenericName,
                  1 as clazz_              
              from
                  Medicine              
              union
              all select
                  Id,
                  Version,
                  Code,
                  Name,
                  null as GenericName,
                  2 as clazz_              
              from
                  AssetEquipment              
              union
              all select
                  Id,
                  Version,
                  Code,
                  Name,
                  null as GenericName,
                  3 as clazz_              
              from
                  [Procedure]              
              union
              all select
                  Id,
                  Version,
                  Code,
                  Name,
                  null as GenericName,
                  4 as clazz_              
              from
                  Supply              
              union
              all select
                  Id,
                  Version,
                  Code,
                  Name,
                  null as GenericName,
                  5 as clazz_              
              from
                  Examination              
              union
              all select
                  Id,
                  Version,
                  Code,
                  Name,
                  null as GenericName,
                  6 as clazz_              
              from
                  OtherItem          
          ) itemalias1_              
              on this_.ItemId=itemalias1_.Id      
      WHERE
          this_.HealthServiceProviderId = @p0;
  
  @p0 = 12 [Type: Int64 (0)]
  </code></pre>
  
  <p>basically what is happening here is im joining to a unionsubclass and on the query its including all subclass of the Item type.</p>
  
  <p>Is there a way to just include the specific type of subclass in a query?</p>
  
  <p>im using mapping by code, below is the mapping for one of the subclass</p>
  
  <pre><code>public class MedicineMap : UnionSubclassMapping&lt;Medicine&gt;
  {
      public MedicineMap()
      {
          Property(p =&gt; p.Code, Rules.CodeRule);
          Property(p =&gt; p.Name, Rules.StrLength255AndNotNull);
          Property(p =&gt; p.GenericName, Rules.StrLength400AndNullable);
  
          Bag(x =&gt; x.HMOMedicineMappings, bag =&gt;
          {
              bag.Inverse(true); 
              bag.Key(k =&gt; k.Column(col =&gt; col.Name("ItemId"))); 
  
          }, a =&gt; a.OneToMany());
  
          Bag(x =&gt; x.HSPMedicineMappings, bag =&gt;
          {
              bag.Inverse(true); 
              bag.Key(k =&gt; k.Column(col =&gt; col.Name("ItemId"))); 
  
  
          }, a =&gt; a.OneToMany());
      }
  }
  </code></pre>
  
  <p>Here is my entities</p>
  
  <pre><code>public abstract class Item : EntityBase
  {
      public virtual string Code { get; set; }
      public virtual string Name { get; set; }
  
  }
  
  public class Medicine : Item
  {
      public Medicine()
      {
          HSPMedicineMappings = new List&lt;HSPMedicineMapping&gt;();
          HMOMedicineMappings = new List&lt;HMOMedicineMapping&gt;();
      }
      public virtual string GenericName { get; set; }
      public virtual IList&lt;HSPMedicineMapping&gt; HSPMedicineMappings { get; set; }
      public virtual IList&lt;HMOMedicineMapping&gt; HMOMedicineMappings { get; set; }
  
  }
  
  
  public class AssetEquipment : Item
  {
      public AssetEquipment()
      {
          HSPAssetEquipmentMappings = new List&lt;HSPAssetEquipmentMapping&gt;();
          HMOAssetEquipmentMappings = new List&lt;HMOAssetEquipmentMapping&gt;();
      }
      public virtual IList&lt;HSPAssetEquipmentMapping&gt; HSPAssetEquipmentMappings { get; set; }
      public virtual IList&lt;HMOAssetEquipmentMapping&gt; HMOAssetEquipmentMappings { get; set; }
  }
  
  public abstract class HSPItemMapping : EntityBase
  {
      public virtual HealthServiceProvider HealthServiceProvider { get; set; }
      public virtual string HSPItemCode { get; set; }
      public virtual string HSPItemName { get; set; }
      public virtual Item Item { get; set; }
  
  
  }
  
  public class HSPMedicineMapping : HSPItemMapping
  {
  
  
  }
  </code></pre>