HQL子查询报错:org.hibernate.hql.ast.QuerySyntaxException: unexpected token...

HQL语句:

select count(*) from (select stu.classId  from Student stu group by stu.classId) t_tmp_count

执行该HQL语句一直报错:org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 22 [select count(*) from (...  

反复检查该HQL语句,没发现问题;执行对应的sql语句,能正确得到结果;直接上网搜这个异常,也没得到多少有用的信息。

最后不得已,只好用Native SQL查询解决问题。  

解决完后马上又想到可以用下面HQL语句解决问题  

select count(distinct stu.classId)  from Student  

只怪自己当时钻牛角尖。

下班后,专门打开《hibernate_reference.pdf》《第14章 HQL: Hibernate查询语言》。猛然瞥见《14.13. 子查询》小节倒数第二段:

Note that HQL subqueries can occur only in the select or where clauses

原来“HQL子查询只能出现在select或where字句中“ 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值