title: NHibernate Eager Fetching Over Multiple Levels with StatelessSession
body: <p>I've referenced the following thread which works with a Session but not a StatelessSession.  <a href="https://stackoverflow.com/questions/332703/nhibernate-eager-fetching-over-multiple-levels">NHibernate Eager Fetching Over Multiple Levels</a> </p>
  
  <p>The problem I am having is similar to the above thread.  I'm getting back 4xApplications and 4xRoles.  I should be getting back 1xApplication and 4xRoles.</p>
  
  <p>Criteria query:</p>
  
  <pre><code>return StatelessSession.CreateCriteria(typeof(Application))
              .SetResultTransformer(Transformers.DistinctRootEntity)
              .SetFetchMode("Roles", FetchMode.Join)
              .List&lt;Application&gt;();
  </code></pre>
  
  <p>My entities and mappings:</p>
  
  <pre><code>public class Application : IComparable&lt;Application&gt;
  {
      public virtual int Id { get;  set; }
      public virtual string InternalName { get; set; }
      public virtual ICollection&lt;Role&gt; Roles { get; set; }
  
      #region IComparable&lt;Application&gt; Members
  
      public virtual int CompareTo(Application other)
      {
          return Id.CompareTo(other.Id);
      }
  
      #endregion
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public class Role : IComparable&lt;Role&gt;
  {
      public virtual int Id { get; set; }
      public virtual string Name { get; set; }
      public virtual Application Application { get; set; }
  
      public virtual int CompareTo(Role other)
      {
          return Id.CompareTo(other.Id);
      }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public class ApplicationMapping : ClassMap&lt;Application&gt;
  {
      public ApplicationMapping()
      {
          Table("Application");
          Id(x =&gt; x.Id, "ID").GeneratedBy.Assigned(); ; //.Column("ID");
          Map(x =&gt; x.InternalName);
          HasMany(x =&gt; x.Roles).Inverse().Cascade.All().AsSet().Not.LazyLoad();
      }
  }
  
  public class RoleMapping : ClassMap&lt;Role&gt;
  {
      public RoleMapping()
      {
  
          Table("Roles");
          Id(x =&gt; x.Id, "ID").GeneratedBy.Assigned(); 
          References(x =&gt; x.Application, "ApplicationID").Not.LazyLoad();
          Map(x =&gt; x.Name);
          HasMany(x =&gt; x.RightsUsers).Table("UserRoleXRef").Inverse().Cascade.All().AsSet();
      }   
  }
  </code></pre>
  
  <p>hbm.xml</p>
  
  <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property"     auto-import="true" default-cascade="none" default-lazy="true"&gt;
    &lt;class xmlns="urn:nhibernate-mapping-2.2" name="Quad.App.Test.DataAccess.NHibernate.Scaffolding.Entities.Application, Quad.App.Test, Version=1.4.0.742, Culture=neutral, PublicKeyToken=null" table="Application"&gt;
  &lt;id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
    &lt;column name="ID" /&gt;
    &lt;generator class="assigned" /&gt;
  &lt;/id&gt;
  &lt;property name="InternalName" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
    &lt;column name="InternalName" /&gt;
  &lt;/property&gt;
  &lt;set cascade="all" inverse="true" lazy="false" name="Roles"&gt;
    &lt;key&gt;
      &lt;column name="ApplicationID" /&gt;
    &lt;/key&gt;
    &lt;one-to-many class="Quad.App.Test.DataAccess.NHibernate.Scaffolding.Entities.Role, Quad.App.Test, Version=1.4.0.742, Culture=neutral, PublicKeyToken=null" /&gt;
  &lt;/set&gt;
     &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property"     auto-import="true" default-cascade="none" default-lazy="true"&gt;
    &lt;class xmlns="urn:nhibernate-mapping-2.2" name="Quad.App.Test.DataAccess.NHibernate.Scaffolding.Entities.Role, Quad.App.Test, Version=1.4.0.742, Culture=neutral, PublicKeyToken=null" table="Roles"&gt;
  &lt;id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
    &lt;column name="ID" /&gt;
    &lt;generator class="assigned" /&gt;
  &lt;/id&gt;
  &lt;property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
    &lt;column name="Name" /&gt;
  &lt;/property&gt;
  &lt;many-to-one class="Quad.App.Test.DataAccess.NHibernate.Scaffolding.Entities.Application, Quad.App.Test, Version=1.4.0.742, Culture=neutral, PublicKeyToken=null" lazy="false" name="Application"&gt;
    &lt;column name="ApplicationID" /&gt;
  &lt;/many-to-one&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>SQL:</p>
  
  <pre><code>SELECT this_.ID as ID1_1_, 
  this_.InternalName as Internal2_1_1_, 
  roles2_.ApplicationID as Applicat3_3_, 
  roles2_.ID as ID3_, 
  roles2_.ID as ID2_0_, 
  roles2_.Name as Name2_0_, 
  roles2_.ApplicationID as Applicat3_2_0_ 
  FROM dbo.Application this_ left outer join dbo.Roles roles2_ on       
  this_.ID=roles2_.ApplicationID
  </code></pre>