2014-10-02 31 views
0

對於HQL,我相當缺乏經驗,所以我想知道是否有人可以幫我解決這個問題。我有一個關係,Student與StudentCollege有一對一的關係。我正在嘗試編寫一個查詢,查找每個已經創建應用程序的學生,以及他們提交了多少個應用程序。我寫下了下面的查詢,但我不確定它是否接近我應該做的。HQL:在查詢中有條件地計算子集合

select distinct new ReportVO (stu.id, stu.first_name, stu.last_name, stu.year, stu.school.school_name, sum(case when si.applied = 1 then 1 else 0 end) as numApplications) " + 
        "from StudentCollege as si join si.student as stu where stu.year <= 12 and stu.user.id = :userId and si.applied = 1 order by stu.last_name 

當它運行以下異常被拋出:

org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [package.location.ReportVO] [select distinct new ReportVO (stu.id, stu.first_name, stu.last_name, stu.year, stu.school.school_name, sum(case when si.applied = 1 then 1 else 0 end) as numApplications) from package.location.StudentCollege as si join si.student as stu where stu.year <= 12 and stu.user.id = :userId and si.applied = 1 order by stu.last_name] 

但是我對VO對象,獲取在相同數量的參數的構造函數,所以我想從返回的類型總和不正確。我也試過更換numApplications整數計數,但也拋出了相同的例外。

public ReportVO(int id, String firstName, String lastName, int year, String school, Integer numApplications) 

任何幫助表示讚賞!

回答

0

我相信你在where子句後面缺少一個「group by」子句。

select distinct new ReportVO (stu.id, stu.first_name, stu.last_name, stu.year, stu.school.school_name, sum(case when si.applied = 1 then 1 else 0 end) as numApplications) " + 
        "from StudentCollege as si join si.student as stu where stu.year <= 12 and stu.user.id = :userId and si.applied = 1 group by stu.id, stu.first_name, stu.last_name, stu.year, stu.school.school_name order by stu.last_name 
+0

不幸的是,添加group by子句產生了相同的異常和消息 - 「無法在類上找到合適的構造函數」我很欣賞這個想法。 – rawkfist0215 2014-10-03 15:37:31