title: NHibernate Filtered Child Collection Lazy Loaded even with eager fetch specified
body: <p>Im trying to find out why a child collection is being returned without filtering even when eager loading the collection and the generated SQL is correct.</p>
  
  <p>The fluent mappings for the classes are:</p>
  
  <pre><code>public class OptionIdentifierMap : ClassMap&lt;OptionIdentifier&gt;
  {
      public OptionIdentifierMap()
          : base("OptionIdentifier")
      {
          //Id Mapping Removed
          HasMany&lt;OptionPrice&gt;(x =&gt; x.OptionPrices)
               .KeyColumn("OptionIdentifier_id")
               .Cascade.None();
      }
  }
  
  public class OptionPriceMap : ClassMap&lt;OptionPrice&gt;
  {
      public OptionPriceMap()
          : base("OptionPrice")
      {
          //Id Mapping removed
          References(x =&gt; x.Option)
               .Column("OptionIdentifier_id")
               .Cascade.None()
               .ForeignKey("FK_OptionPrice_OptionIdentifier_id_OptionIdentifier_Id")
               .Not.Nullable();
          References(x =&gt; x.Increment)
               .Column("PricingIncrement_id")
               .Cascade.None()
               .ForeignKey("FK_OptionPrice_PricingIncrement_id_PricingIncrement_Id")
               .Not.Nullable();
          Map(x =&gt; x.Price).Not.Nullable();
      }
  }
  </code></pre>
  
  <p>and PricingIncrement mapping</p>
  
  <pre><code>public class PricingIncrementMap : ClassMap&lt;PricingIncrement&gt;
  {
      public PricingIncrementMap()
          : base("PricingIncrement")
      {
          Map(x =&gt; x.IncrementYear);
          HasMany&lt;OptionPrice&gt;(x =&gt; x.Options)
               .KeyColumn("PricingIncrement_id")
               .Cascade.None().Inverse();
      }
  }
  </code></pre>
  
  <p>And the Entities are:</p>
  
  <pre><code>public class PricingIncrement : Entity
  {
      public PricingIncrement()
      {
          Options = new List&lt;OptionPrice&gt;();
      }
  
      public virtual int IncrementYear { get; set; }
      public virtual IList&lt;OptionPrice&gt; Options { get; set; }
  }
  
  public class OptionPrice : Entity
  {
      public OptionPrice()
      {
      }
  
      public virtual OptionIdentifier Option { get; set; }
      public virtual PricingIncrement Increment { get; set; }
      public virtual float Price { get; set; }
  }
  
  public class OptionIdentifier : Entity
  {
      public OptionIdentifier()
      {
          OptionPrices = new List&lt;OptionPrice&gt;();
      }
              public virtual IList&lt;OptionPrice&gt; OptionPrices { get; set; }
  }
  </code></pre>
  
  <p>Im trying to query All the OptionIdentifier that have an optionprice value for an specific PricingIncrement.
  The SQL Query that nhibernate generates from my criteria is:</p>
  
  <pre><code>SELECT this_.Id                      as Id37_4_,
     .......
  FROM   OptionIdentifier this_       inner join OptionPrice op2_         on this_.Id = op2_.OptionIdentifier_id
     inner join PricingIncrement i3_         on op2_.PricingIncrement_id = i3_.Id
  WHERE  (this_.IsDeleted = 0)
     and this_.Id in (7)
     and i3_.IncrementYear = 2015
  </code></pre>
  
  <p>The criteria I'm using to build this query is:</p>
  
  <pre><code>ICriteria pagedCriteria = this.Session.CreateCriteria&lt;OptionIdentifier&gt;()
                  .CreateAlias("OptionPrices", "op", JoinType.InnerJoin)
                  .CreateAlias("op.Increment", "i", JoinType.InnerJoin)
                  .SetFetchMode("op", FetchMode.Eager)
                  .SetFetchMode("i", FetchMode.Eager)
                  .Add(Restrictions.Eq("i.IncrementYear", 2015))
                  .Add(Expression.In("Id", idList.ToList&lt;int&gt;()))
                  .SetResultTransformer(CriteriaSpecification.DistinctRootEntity);
  </code></pre>
  
  <p>When looking at SQL Profiler, the query executes and the result is correct, i get one row for each child in the OptionPrice table that matches the criteria, in my case one, from the available 4 rows that match the OptionIdentifier (there are 4 rows in PricingIncrement and 4 in OptionPrice one for each PricingIncrement for the OptionIdentifier_id 7)</p>
  
  <p>But when i try to iterate the collection to get some values, for some reason nhibernate is loading the child collection, as if lazy load was specified, and loading the full 4 child rows. Reading the documentation FetchMode is supposed to fix this preventing nhibernate to lazy load child collections. Similar to a N+1 common issue.</p>
  
  <p>I checked the SQL Profiler to see whats happening and nhibernate is generating queries without the original filter to fill the child collection when i try to access it. If i dont access the collection no query is generated.</p>
  
  <p>Doing some testing i tried different join types and fetch modes, and so far the only way to iterate the collection without having hibernate load all the elements is to specify in the join type LeftOuterJoin, but this means something different.</p>
  
  <p>I tried to search for issues similar but all of them say that eager loading should work, or mention that i should use filters. And so far i havent found any answer.</p>
  
  <p>Any help is greatly appreciated.</p>