2014-10-02 29 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