hibernate:sql没问题,却一直报错

类似的sql:select count(distinct(seller_id)) from flow;在数据库工具可以执行,用hibernate就不能执行报错

解决方式:改成select count(distinct seller_id ) from flow;(当然这个语句在数据库工具里也可以执行)

报错:Caused by: 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字句中“ 

posted @ 2016-12-06 11:25  刘尊礼  阅读(1561)  评论(0)    收藏  举报