views:

135

answers:

2

I have the following class structure:

class TestCase {
  public IList<Step> Steps { get; set; }
}

class Step { 
  public IList<Action> Actions { get; set; }
}

class Action { }

I want to load a TestCase, all Steps, and all Events in one query and avoid the Select N+1 problem.

This post solves the problem of loading the Steps with TestCases (using IMultiQuery), but how would I load the Actions too?

A: 

You can do exactly what that referenced post discussed. Just Add the 3rd criteria onto the 2nd criteria, instead of both of them on the 1st criteria.

var result = session.CreateMultiCriteria()
            .Add(criteria1
                .Add(criteria2)
            )
            .List();
Mufasa
+1  A: 

You need to be careful of your cross product here. To handle more than one level deep for eager fetching, you'll need to use a <set> instead of a <bag> or <list>. This will guarantee that you don't have duplicate 'Steps' in your Test Case. Finally, to get this to work use the following:


var result =  session.CreateCriteria(typeof (TestCase))
                .CreateAlias("Steps", "s")
                .CreateAlias("s.Actions", "a")
                .SetResultTransformer(CriteriaUtil.DistinctRootEntity);
                .List();
Trent