views:

1549

answers:

2

I've got a many-to-many relationship which I try to fetch eager:

*.CreateCriteria(typeof(Class1))
.SetFetchMode("Class2", FetchMode.Eager)
.SetResultTransformer(new DistinctRootEntityResultTransformer())
.SetFirstResult(20)
.SetMaxResult(10)
.List<Class1>();

I'd like to have rows 20-30 returned, but instead I've got 12-18. Why? Because the SetResultTransformer is executed AFTER the SetMaxResult. It returns 10 rows starting from row 20, but then it is distincted (is that a word?) resulting in rows 12-18. I totally understand what's happening, but can't think of a solution using criteria...

Does anyone have a solution to that?

A: 

I did not actually try this, but it might work.

  *.CreateCriteria(typeof(Class1))
    .SetFirstResult(20)
    .SetMaxResult(10)
    .CreateCriteria("Class2", JoinType.LeftOuterJoin)
    .SetResultTransformer(new DistinctRootEntityResultTransformer())
    .List<Class1>();

EDIT: This does not work. Use multiquery, multicriteria (or Future when using a new build of NH)

Paco
I tried it. It doesn't work. It has the exact same problem as the original post.
Nathan
I know now, it's not possible because it's a very inefficient query. You should use multiquery
Paco
Could you provide a few more details (preferably examples specific to this question) on how to use the multicriteria, multiquery, and Future features? I'm also having the same problem, and I'm not quite sure how to go about using the features mentioned to accomplish the goal of this question.
Nathan
Nathan, you might like to read this:http://nhforge.org/blogs/nhibernate/archive/2008/09/06/eager-loading-aggregate-with-many-child-collections.aspx
Paco
A: 

I have the same problem. An adaptation of the following using detached criteria and subqueries looks like it might be promising. I'm going to do a more thorough trial of it when I get some time.

http://blogs.taiga.nl/martijn/2008/11/20/nhibernate-criteria-queries-across-multiple-many-many-associations/

Nathan