NHibernate:最简单的方法来返回带有分页子集的对象? [英] NHibernate: Simplest way to return an object with paged children collection?

查看:101
本文介绍了NHibernate:最简单的方法来返回带有分页子集的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想返回一个Parent对象,其中的childs集合急切地加载了请求的child对象页面(子集).实现此目标的最佳方法是什么?过滤器? ICriteria查询可以吗?

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?

我正在使用.SetFirstResult()和.SetMaxResults()进行分页以收集汇总根结果,但是是否可以将其与汇总根一起使用以选择子级结果页面?

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?

遵循以下原则:

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.
}


更新:

实际上,急切的加载要求并不是那么重要.我只希望在访问它们时加载子对象的分页子集.

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:

  • Use a query to get the (paged) parents and one to load all the collections for those parents (read http://ayende.com/Blog/archive/2010/01/16/eagerly-loading-entity-associations-efficiently-with-nhibernate.aspx for some concepts)
  • Set 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-2316

更新(针对新要求):

正如您所阅读的,您可以使用session.CreateFilter来过滤/排序/分页子集合.这行得通,并且在任何地方都受支持.

As you've read, you can use session.CreateFilter to filter/sort/page a child collection. This works and it is supported everywhere.

此外,还有一个补丁程序( NH-2319 ;我将转换为插件,因为不太可能在主干中接受),因此可以使用Linq.它仅限于某些集合类型,并且要求关系是双向的,但允许以下操作:

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();

这篇关于NHibernate:最简单的方法来返回带有分页子集的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆