NHibernate:如何在投影中选择根实体 [英] NHibernate: How to select the root entity in a projection
问题描述
Ayende在这里描述了一种获取页数的绝佳方法,并在单个查询中提供了特定数据页:
Ayende describes a really great way to get page count, and a specific page of data in a single query here:
http://ayende. com/blog/2334/paged-data-count-with-nhibernate-really-easy-way
他的方法如下:
IList list = session.CreateQuery("select b, rowcount() from Blog b")
.SetFirstResult(5)
.SetMaxResults(10)
.List();
唯一的问题是此示例在HQL中,并且我需要在ICriteria查询中执行相同的操作.为了达到与ICriteria相同的效果,我需要做类似的事情:
The only problem is this example is in HQL, and I need to do the same thing in an ICriteria query. To achieve the equivalent with ICriteria, I need to do something like:
IList list = session.CreateCriteria<Blog>()
.SetFirstResult(5)
.SetMaxResults(10)
.SetProjection(Projections.RootEntity(), Projections.SqlFunction("rowcount", NHibernateUtil.Int64))
.List();
问题在于没有Projections.RootEntity()之类的东西.有什么方法可以选择根实体作为投影列表中的投影之一?
The problem is there is no such thing as Projections.RootEntity(). Is there any way to select the root entity as one of the projections in a projection list?
是的,我知道我可以只使用CriteriaTransform.TransformToRowCount(),但这将需要执行两次查询-一次用于结果,一次用于行数.使用Futures可以减少一次往返,但可能会有所帮助,但是它仍然在SQL Server上执行两次查询.对于密集查询,这是不可接受的.我想避免开销,并在同一查询中返回行数和结果.
Yes, I know I could just use CriteriaTransform.TransformToRowCount() but that would require executing the query twice - once for the results and once for the row count. Using Futures may help a little by reducing it to one round-trip, but it's still executing the query twice on the SQL Server. For intensive queries this is unacceptable. I want to avoid the overhead, and return the row count and the results in the same query.
基本问题是:使用ICriteria,是否可以同时选择根实体和其他一些投影?
The basic question is: using ICriteria, is there any way to select the root entity AND some other projection at the same time?
一些相关链接:
https://nhibernate .jira.com/browse/NH-1372?jql = text%20〜%20%22entity%20projection%22
https://nhibernate.jira.com/browse/NH-928
推荐答案
我实现了RootEntityProjection.您可以尝试在以下位置找到该代码:
I implemented a RootEntityProjection. You can try the code, which you can find at: http://weblogs.asp.net/ricardoperes/archive/2014/03/06/custom-nhibernate-criteria-projections.aspx. Let me know if it helped.
这篇关于NHibernate:如何在投影中选择根实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!