Java HQL:在查询中有条件地计算子集合

Java HQL:在查询中有条件地计算子集合,java,sql,database,hibernate,hql,Java,Sql,Database,Hibernate,Hql,我在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

我在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

感谢您的帮助

我相信您在where子句之后缺少了一个“groupby”子句

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
选择不同的新报告VO(stu.id、stu.first\u name、stu.last\u name、stu.year、stu.school.school\u name、sum(当si.applicated=1,则为1,否则为0)作为numApplications)+

“从StudentCollege作为si加入si.student作为stu,其中stu.year不幸地添加了group by子句,产生了相同的异常和消息——“无法在课堂上找到合适的构造函数”,尽管如此,我还是很欣赏这个想法。
public ReportVO(int id, String firstName, String lastName, int year, String school, Integer numApplications)
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