jon*_*onh 5 nhibernate projection rowcount data-paging nhibernate-criteria
Ayende描述了一种非常好的获取页面计数的方法,以及一个查询中的特定数据页面:
http://ayende.com/blog/2334/paged-data-count-with-nhibernate-the-really-easy-way
他的方法看起来像:
IList list = session.CreateQuery("select b, rowcount() from Blog b")
.SetFirstResult(5)
.SetMaxResults(10)
.List();
Run Code Online (Sandbox Code Playgroud)
唯一的问题是这个例子是在HQL中,我需要在ICriteria查询中做同样的事情.要达到与ICriteria相当的水平,我需要做类似的事情:
IList list = session.CreateCriteria<Blog>()
.SetFirstResult(5)
.SetMaxResults(10)
.SetProjection(Projections.RootEntity(), Projections.SqlFunction("rowcount", NHibernateUtil.Int64))
.List();
Run Code Online (Sandbox Code Playgroud)
问题是没有Projections.RootEntity()这样的东西.有没有办法选择根实体作为投影列表中的投影之一?
是的,我知道我可以使用CriteriaTransform.TransformToRowCount(),但这需要执行两次查询 - 一次用于结果,一次用于行计数.使用Futures可以通过将其减少到一次往返来帮助一点,但它仍然在SQL Server上执行两次查询.对于密集查询,这是不可接受的.我想避免开销,并在同一查询中返回行计数和结果.
基本问题是:使用ICriteria,有没有办法同时选择根实体和其他一些投影?
编辑:一些相关链接:
https://nhibernate.jira.com/browse/NH-1372?jql=text%20~%20%22entity%20projection%22
我实现了一个 RootEntityProjection。您可以尝试以下位置的代码:http://weblogs.asp.net/ricardoperes/archive/2014/03/06/custom-nhibernate-criteria-projections.aspx。让我知道是否有帮助。
| 归档时间: |
|
| 查看次数: |
1343 次 |
| 最近记录: |