Hibernate Criteria来自单个查询的多个rowCounts

时间:2013-11-07 16:24:11

标签: java hibernate criteria

我不确定这是否可行,但我想知道是否有办法创建单个条件查询并根据唯一限制返回多个计数。

实施例

    Criteria criteria = session.createCriteria(EmployeeProfile.class);

    Integer pAccrualBalance = ((Number)criteria
            .add(Restrictions.gt("pAccrualBalance", BigDecimal.ZERO))
            .setProjection(Projections.rowCount()).uniqueResult()).intValue();

    Integer employeeCount = ((Number)criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();

    logger.info("verify pAccrualBalance import count " + pAccrualBalance);

    logger.info("verify employee import count " + employeeCount);

此代码的问题是pAccrualBalance限制限制了employeeCount结果集。我想获得employeeCount的无限制计数,而无需进行单独的查询。

1 个答案:

答案 0 :(得分:0)

你需要两次加入桌面......虽然这可以通过Criteria完成,但在HQL中通常更容易。

select count(ep1.id), count(ep2.id)
from EmployeeProfile as ep1
join EmployeeProfile as ep2
where ep2.pAccrualBalance > 0