[英]Hibernate row count on Criteria with already set Projection
For a grid component I have in my web applications I have a "GridModel" class which gets passed a Criteria. 对于Web应用程序中的网格组件,我有一个“ GridModel”类,该类通过了Criteria。
The GridModel class has a method to get the results for a specific page by adding setFirstResult(...)
and setMaxResults(...)
to the Criteria. GridModel类具有一种方法,该方法通过将
setFirstResult(...)
和setMaxResults(...)
到Criteria来获取特定页面的结果。
But I also need the total count of rows for the Criteria, so I have the following method: 但是我还需要条件的总行数,因此我有以下方法:
public int getAvailableRows() {
Criteria c = criteriaProvider.getCriteria();
c.setProjection(Projections.rowCount());
return((Long)c.uniqueResult()).intValue();
}
This worked perfectly, but now I have a grid that requires a Criteria that already uses setProjection()
in combination with setResultTransformer()
. 这完美地工作了,但是现在我有了一个网格,该网格要求已经使用
setProjection()
和setResultTransformer()
的Criteria。 It seems that the getAvailableRows()
method above overrides
the setProjection()
of the original Criteria creating wrong results. 似乎上面的
getAvailableRows()
方法overrides
原始Criteria的setProjection()
,从而产生错误的结果。
Can I wrap a count Criteria around the original Criteria instead somehow? 我可以以某种方式将计数标准包裹在原始标准周围吗? Or how would I solve this?
或我该如何解决?
I've had a similar experience when trying to use the Projections.rowCount()
in conjunction with a groupBy
expression. 尝试将
Projections.rowCount()
与groupBy
表达式结合使用时,我也有类似的经历。 I was able to circumvent things in a slightly 'hacky' manner by: 通过以下方式,我能够以一种“变态”的方式规避事物:
Restore the previous projection + transformer so the Criteria can be used for actual result retrieving if 恢复以前的投影+转换器,以便在以下情况下可以将“标准”用于实际结果检索:
final Projection originalProjection = criteriaImpl.getProjection(); final ResultTransformer originalResultTransformer = criteriaImpl.getResultTransformer(); final Projection rowCountProjection; // If we identify that we have a function with a group by clause // we need to handle it in a special fashion if ( originalProjection != null && originalProjection.isGrouped() ) { final CriteriaQueryTranslator criteriaQueryTranslator = new CriteriaQueryTranslator( (SessionFactoryImplementor)mySessionFactory, criteriaImpl, criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS ); rowCountProjection = Projections.projectionList() .add( Projections.rowCount() ) .add( Projections.sqlGroupProjection( // This looks stupid but is seemingly required to ensure we have a valid query "count(count(1))", criteriaQueryTranslator.getGroupBy(), new String[]{}, new Type[]{} ) ); } else { rowCountProjection = Projections.rowCount(); } // Get total count of elements by setting a count projection final Long rowCount = (Long)criteria.setProjection( rowCountProjection ).uniqueResult();
A few caveats here: 这里有一些警告:
sum
projection as that is not considered an isGrouped()
projection - it will splat the sum with a count. sum
投影的条件,这仍然不会给出预期的结果,因为这不被视为isGrouped()
投影-它将使总和带有计数。 I don't consider this an issue because getting the rowcount for an expression of that nature probably doesnt make sense
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.