首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >HQL:在查询中有条件地计数子集合

HQL:在查询中有条件地计数子集合
EN

Stack Overflow用户
提问于 2014-10-02 22:04:35
回答 1查看 391关注 0票数 0

对于HQL,我相当缺乏经验,所以我想知道是否有人能帮我解决这个问题。我有一个学生和StudentCollege有OneToMany关系的关系。我正在尝试编写一个查询,查找每个提出申请的学生,以及他们提交了多少个申请。我编写了下面的查询,但我不确定它是否接近我应该做的事情。

代码语言:javascript
运行
复制
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

当它运行时,将引发以下异常:

代码语言:javascript
运行
复制
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替换IntegerCount,但是也会引发相同的异常。

代码语言:javascript
运行
复制
public ReportVO(int id, String firstName, String lastName, int year, String school, Integer numApplications)

任何帮助都是非常感谢的!

EN

回答 1

Stack Overflow用户

发布于 2014-10-02 23:00:24

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

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26170370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档