在休眠状态下不能一次使用where和groupby

时间:2016-06-16 07:16:42

标签: java hibernate

LeaveJournalTable

emp_code  emp_name  leave_code  leave_des     leave_debit  leave_credit

     11        X        AL       openingbalace       5              0

     11        X        EL        openingbalace      8              0

     11        X        EL        allowance          10             0

     11        X        AL       allowance          10              0
     11        X        AL         Taken             0              2
     11        X        AL         Taken             0              3

我想在Hibernate中创建一个查询

Query query = sessionfactory.getCurrentSession().createQuery("from LeaveOpenningJournal WHERE empCode = :empCode and " + "empName = :empName and " +  "companyCode = :companyCode") 

以及我想总结leave_credit字段GroupBy(leave_code,leave_des)

1 个答案:

答案 0 :(得分:1)

请检查此代码:

Query listQuery = session.createSQLQuery("SELECT *,SUM(leave_credit) FROM LeaveOpenningJournal j WHERE mpCode = :empCode AND empName = :empName GROUP BY leave_code,leave_des");
listQuery.setParameter("empCode", "11");
listQuery.setParameter("empName", "x");
List<Object[]> parentChildMap = listQuery.list();

只需将parameter和获取结果设置为list