简体   繁体   中英

Hibernate Criteria multiple rowCounts from a single query

I'm not sure if this is possible, but I'm wondering if there is a way to create a single criteria query and return multiple counts based on unique restrictions.

Example

    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);

The problem with this code is the pAccrualBalance restriction restricts the employeeCount result set. I'd like to get the unrestricted count for employeeCount without having to do a separate query.

You'll need to join to the table twice... Whilst this can be done with Criteria, it's often easier in HQL.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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