我有一个mysql查询:
SELECT
SUM(status_days_count)
FROM
(SELECT
cc.status_days_count
FROM
foo cc
JOIN bar c ON c.id = cc.credit_id
WHERE
c.borrower_id = '1'
AND cc.status = 'EXPIRED'
ORDER BY cc.calculation_date
LIMIT 2) AS status_days_count;
它工作正常,但我需要将其映射到休眠条件或命名查询, 但是在分离标准和命名查询中我都不能使用限制,有没有办法在不创建两个查询的情况下实现它?
现在我只出现了这样的事情:
Criteria lastTwoCredits = getHibernateSession().createCriteria(Bar.class);
lastTwoCredits.addOrder(Order.desc("dateReceived"));
lastTwoCredits.add(Restrictions.eq("borrowerId", borrowerId));
lastTwoCredits.setProjection(Projections.property("id"));
lastTwoCredits.setMaxResults(2);
List<Long> ids = criteriaList(lastTwoCredits);
Criteria criteria = createCriteria();
criteria.add(Restrictions.eq("credit.borrowerId", borrowerId));
criteria.add(Restrictions.eq("status", creditStatus));
criteria.add(Restrictions.in("creditId", ids));
criteria.setProjection(Projections.sum("statusDaysCount"));
Object result = criteria.uniqueResult();
建议如何在一个查询中执行此操作非常感谢