我有以下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');
答案 0 :(得分:6)
从大多数内部查询开始创建相对DetachedCriteria
并使用Subqueries.in()
附加到外部查询
对于count(distinct)
使用Projections.countDistinct(propertyName)
。
享受。
答案 1 :(得分:2)
使用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