在HQL方面,我相当缺乏经验,所以我想知道是否有人可以帮助我解决这个问题。我有一个关系,学生与StudentCollege有一个OneToMany关系。我试图编写一个查询,查找已经创建了应用程序的每个学生,以及他们提交了多少个应用程序。我写了下面的查询,但我不确定它是否接近我应该做的事情。
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对象的构造函数,它接受相同数量的参数,所以我认为从 sum 返回的类型是不正确的。我还尝试将 numApplications 替换为整数和计数,但会抛出相同的异常。
public ReportVO(int id, String firstName, String lastName, int year, String school, Integer numApplications)
感谢任何帮助!
答案 0 :(得分: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