关于hql语句使用聚集函数的问题

select sum(counts) from(select count(c) as counts from ChooseJudge c where 1=1 union all select count(f) as counts from FillVacancy f where 1=1)
在数据库运行正常,在java中就报错
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 26 [select count(counts) from(select count(c) as counts from com.zjtl.manage.entity.ChooseJudge c where 1=1 union all select count(f) as counts from com.zjtl.manage.entity.FillVacancy f where 1=1)]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:101)

1个回答

HQL 语句不支持 union 连接
更不知道你的 counts, 它只会把你的 counts 作为对象去寻找,是找不到pojo类的;
所以,你还是使用 native SQL 吧

shushali123
shushali123 已经解决好了,谢谢您
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问