简体   繁体   English

已设置投影的条件中的休眠行数

[英]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: 通过以下方式,我能够以一种“变态”的方式规避事物:

  1. Remembering the previous projection and result transformer 记住先前的投影和结果转换器
  2. Setting the projection on the Criteria to be a modified version (see below) 将“标准”上的投影设置为修改后的版本(请参见下文)
  3. Perform the row count DB hit 执行行计数数据库命中
  4. 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: 这里有一些警告:

  1. This still wont give the expected results if you try and give it a criteria with a single 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 我不认为这是个问题,因为获取这种性质的表达式的行数可能没有任何意义
  2. When I was dealing with this I wrote some unit tests to make sure rowcount was as expected without projections, with property based projections and with groupby projections but I've written this from memory so can't guarantee small kinks won't need ironing out 当我处理此问题时,我编写了一些单元测试来确保没有计数,基于属性的投影和具有groupby投影的行数符合预期,但是我已经从内存中编写了此代码,因此无法保证不需要扭结

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM