用于查找不同值的计数的Hibernate标准

时间:2014-03-04 06:38:53

标签: hibernate

我有以下sql查询,我需要获得相应的hibernate条件查询

SELECT COUNT(DISTINCT employee_id) FROM erp_hr_payment WHERE payment_id IN(
SELECT payment_id FROM erp_hr_payment_collection WHERE payment_id IN(
SELECT payment_id FROM erp_hr_payment_collection WHERE payment_id IN(
SELECT payment_id FROM erp_hr_payment
 WHERE for_month BETWEEN '2013-04-01' AND '2014-03-31' AND arrear_flag=0
) AND element_name='EPF' ) AND element_name='EPFV');

2 个答案:

答案 0 :(得分:6)

从大多数内部查询开始创建相对DetachedCriteria并使用Subqueries.in()附加到外部查询 对于count(distinct)使用Projections.countDistinct(propertyName)。 享受。

答案 1 :(得分:2)

  • 创建分离标准。每个内部查询一个。
  • 将分离的标准放入" in"主要标准的条款
  • 使用countDistinct投影获得不同的员工数量

    DetachedCriteria erp_hr_paymentDetachedCriteria = DetachedCriteria.forClass(Erp_hr_payment.class,"erp_hr_payment");
    erp_hr_paymentDetachedCriteria.setProjection(Property.forName("payment_id"));
    
    Criteria erp_hr_paymentMainCriteria = session.createCriteria(erp_hr_payment.class, "pogHeader");
    erp_hr_paymentMainCriteria.setProjection(Projections.projectionList().add(Projections.alias(Projections.countDistinct("erp_hr_payment.employee_id"), "distinctEmployeeCount")));
    erp_hr_paymentMainCriteria.add(Property.forName("erp_hr_payment.payment_id").in(erp_hr_paymentDetachedCriteria));
    erp_hr_paymentDetachedCriteria.list(); //Will give you your result