对于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替换Integer和Count,但是也会引发相同的异常。
public ReportVO(int id, String firstName, String lastName, int year, String school, Integer numApplications)任何帮助都是非常感谢的!
发布于 2014-10-02 23:00:24
我相信您在where子句之后缺少了一个“组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_namehttps://stackoverflow.com/questions/26170370
复制相似问题