Nhibernate - 分页和急切加载实体

时间:2010-08-22 12:05:40

标签: nhibernate paging eager-loading icriteria fetching-strategy

我有两个实体,一个团队和一个员工。

我希望得到一个拥有热心团队的员工名单。该列表必须被分页。

public PagedList<Employee> GetAllEmployeesWithEagerLoadedTeams(int page, int pageSize)
{
    var criteria = GetSession()
        .CreateCriteria(typeof (Employee))
        .SetFetchMode(DomainModelHelper.GetAssociationEntityNameAsPlural(typeof (Team)),FetchMode.Eager);

    var totalCount = criteria
        .SetProjection(Projections.RowCount())
        .FutureValue<Int32>().Value;

    return criteria
        .SetFirstResult(page * pageSize)
        .SetMaxResults(pageSize)
        .Future<Employee>()
        .ToPagedList(page, pageSize, totalCount);
}

为什么这不起作用?

SQL结果:

SELECT count(*) as y0_ FROM [Employee] this_;

SELECT TOP 10 y0_ FROM (SELECT count(*) as y0_, 
    ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM [Employee] this_) as query WHERE query.__hibernate_sort_row > 10 ORDER BY query.__hibernate_sort_row;

1 个答案:

答案 0 :(得分:2)

您正在尝试重用已经指定了计数投影和未来值的条件变量。 您需要为第二个查询创建新条件。