I want to return one Parent object with the children collection eagerly loaded with the requested page (subset) of children objects. What's the best way to achieve this? Filters? Is is possible with an ICriteria query?
I'm using .SetFirstResult() and .SetMaxResults() to do paging for collections of aggregate root results, but is it possible to utilize this withing the aggregate root to select the page of children results?
Something along these lines:
public class Parent{
int Id;
IList<Child> Children;
}
public Parent GetWithPagedChildren(int id, int page, int pageSize, out int count)
{
//Query
return Parentresult; //With one page of children populated.
}
UPDATE:
Actually, the eagerly loading requirement is not so important. I just want the paged subset of the child objects loaded when I access them.
You can't page and join-fetch child collections in the same query. But you can:
batch-size
on the collections to your page size. This will accomplish more or less the same, but automatically. There's even a patch that allows changing this dynamically: https://nhibernate.jira.com/browse/NH-2316Update (for the new requirements):
As you've read, you can use session.CreateFilter
to filter/sort/page a child collection. This works and it is supported everywhere.
Additionally, there's a patch (NH-2319; I will convert to an addin, as it's unlikely to be accepted in the trunk) that allows using Linq for that. It's limited to some collection types and requires the relationship to be bidirectional, but allows the following:
var parent = GetParent();
var secondPageOfChildrenByName = parent.Children.AsQueryable()
.OrderBy(c => c.Name)
.Skip(PageSize * 1)
.Take(PageSize)
.ToList();