How to implement paging in NHibernate with a left join query

Posted by Gabe Moothart on Stack Overflow See other posts from Stack Overflow or by Gabe Moothart
Published on 2010-03-26T21:16:45Z Indexed on 2010/03/26 21:33 UTC
Read the original article Hit count: 423

Filed under:
|
|

I have an NHibernate query that looks like this:

        var query = Session.CreateQuery(@"
              select o
              from Order o
                left join o.Products p
              where
                (o.CompanyId = :companyId) AND
                (p.Status = :processing)
              order by o.UpdatedOn desc")
              .SetParameter("companyId", companyId)
              .SetParameter("processing", Status.Processing)
              .SetResultTransformer(Transformers.DistinctRootEntity);

        var data = query.List<Order>();

I want to implement paging for this query, so I only return x rows instead of the entire result set.

I know about SetMaxResults() and SetFirstResult(), but because of the left join and DistinctRootEntity, that could return less than x Orders.

I tried "select distinct o" as well, but the sql that is generated for that (using the sqlserver 2008 dialect) seems to ignore the distinct for pages after the first one (I think this is the problem).

What is the best way to accomplish this?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about hql