title: Nhibernate: eager loading two child collections (one being a component list)
body: <p>i have a Parent class that has two child collections ChildCollectionA and ChildCollectionB.
  ChildCollectionA is mapped as an association, and has its own ID:</p>
  
  <pre><code>HasMany(parent =&gt; parent.ChildCollectionA)
  .KeyColumn("IDParent")
  .AsBag().Cascade.AllDeleteOrphan();
  </code></pre>
  
  <p>and ChildCollectionB is mapped has a component list:</p>
  
  <pre><code>HasMany(parent =&gt; parent.ChildCollectionB)
      .Table("ChildCollectionBTable")
      .KeyColumn("IDParent")
      .Component(m=&gt;
                  {
                      m.References(childB =&gt; childB.Task, "IDTask").Not.LazyLoad().Not.Nullable();
                      m.Map(childB  =&gt; childB.Date, "Date").Not.Nullable();
  
                  } 
          )
      .AsBag().Cascade.AllDeleteOrphan();
  </code></pre>
  
  <p>I need now all the Parents in the DataBase, because i will have to perform some operations that need both ChildCollectionA and ChildCollectionB.</p>
  
  <p>So I had to eager load them, I used fetch mode to eager load ChildCollectionA first:</p>
  
  <p>    var queryParents = session.CreateCriteria()
          .SetFetchMode("ChildCollectionA", FetchMode.Eager)
          .Add(Expression.Le("ParentDate",endDate));</p>
  
  <p>It returned 492 Parents (should be 481), the total value of the operation i performed was 32,847.46€ (should be 30,790.87€). So i has to eliminate Parent duplicates:</p>
  
  <pre><code>var queryParents = session.CreateCriteria&lt;Parent&gt;()
      .SetFetchMode("ChildCollectionA", FetchMode.Eager)
      .Add(Expression.Le("ParentDate",endDate))
      .SetResultTransformer(new DistinctRootEntityResultTransformer());
  </code></pre>
  
  <p>I tried the same eager loading with just the ChildCollectionB</p>
  
  <pre><code>var queryParents = session.CreateCriteria&lt;Parent&gt;()
      .SetFetchMode("ChildCollectionB", FetchMode.Eager)
      .Add(Expression.Le("ParentDate",endDate))
      .SetResultTransformer(new DistinctRootEntityResultTransformer());
  </code></pre>
  
  <p>In both cases returned 481 parents OK, and the value was 30,790.87€ OK.</p>
  
  <p>But I needed to eager load both collections at the same time, I did this:</p>
  
  <pre><code>var queryParents = session.CreateCriteria&lt;Parent&gt;()
      .SetFetchMode("ChildCollectionA", FetchMode.Eager)
      .SetFetchMode("ChildCollectionB", FetchMode.Eager)
      .Add(Expression.Le("ParentDate",endDate))
      .SetResultTransformer(new DistinctRootEntityResultTransformer());
  </code></pre>
  
  <p>It returned 481 parents OK, and the value was 32,602.57€ (should be 30,790.87€).</p>
  
  <p>Now the number of parents returned is correct, but there are duplicates somewhere else, the values depend on the collections and not the parent, so the duplicates must be somewhere in the ChildCollections.</p>
  
  <p>Right now i'm using an ugly solution:</p>
  
  <pre><code>var queryParents = session.CreateCriteria&lt;Parent&gt;()
      .SetFetchMode("ChildCollectionA", FetchMode.Eager)
      .Add(Expression.Le("ParentDate",endDate))
      .SetResultTransformer(new DistinctRootEntityResultTransformer());
  
  parents= queryParents.List&lt;Parent&gt;();
  
    foreach (Parent p in parents)
    {
        NHibernateUtil.Initialize(p.ChildCollectionB);
    }
  </code></pre>
  
  <p>It returned 481 parents OK, and the value was 30,790.87€ OK.</p>
  
  <p>The problem happens when i eager load both collections, if i eager load just one, and then force the lazyload to the other, its works. I don't know if the mapping of ChildCollectionB being a component list instead of an association has something to do with this...</p>
  
  <p>Any clues?</p>
  
  <p>Thanks</p>