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子查询只能出现在selectwhere字句中“ 


已标记关键词 清除标记
以下是我在开发ssh中出现的异常,请各位帮忙解决一下 org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: 5 near line 1, column 12 [select top 5 * from com.no9.entity.Goods order by PutTime desc]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: 5 near line 1, column 12 [select top 5 * from com.no9.entity.Goods order by PutTime desc] at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:639) at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:377) at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:840) at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:832) at com.no9.dao.impl.GoodsDAO.findTopByTime(GoodsDAO.java:194) at com.no9.biz.impl.GoodsBizImpl.findTopByPubTime(GoodsBizImpl.java:39) at org.apache.jsp.right_jsp._jspService(right_jsp.java:78) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630) at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:535) at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:472) at org.apache.struts2.components.Include.include(Include.java:254) at org.apache.struts2.components.Include.end(Include.java:166) at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42) at org.apache.jsp.index_jsp._jspx_meth_s_005finclude_005f3(index_jsp.java:173) at org.apache.jsp.index_jsp._jspService(index_jsp.java:82) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Unknown Source) 相关代码如下----DAO层 //通过时间查询最新发布的产品 public List<Goods> findTopByTime(){ log.debug("finding top By Time Goods instances"); try { String queryString = "select top 5 * from Goods order by PutTime desc"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } BIZ层 //查询最新发布 5条记录 public List<Goods> findTopByPubTime(){ List<Goods> listTopTime = gooddao.findTopByTime(); if(listTopTime != null && listTopTime.size() > 0){ System.out.println(listTopTime.size()); return listTopTime; }else{ return null; } } 页面代码为: <% //查询最新发布--- GoodsBizImpl topPubTime = (GoodsBizImpl)SpringBeanUtil.getInstance().getBean("GoodsBiz"); List<Goods> listTopTime = topPubTime.findTopByPubTime(); session.setAttribute("topTime",listTopTime);//将手机信息保存到session中 %> <div style="height:20px; background-image:url(images/newfoot_bg.gif)"> <a href="#">最新发布</a> </div> <div class="frameBorder" style="width:150px"> <s:iterator id="topTime" value="#session.topTime" status="stuts"> <div> <a href="#"><img src="images/products/<s:property value="images" />"/></a> <s:property value="goodName" /> </div> </s:iterator> </div> 现在一时找不到原因,望各位给予解决,谢谢 [b]问题补充:[/b] 在DAO层的sql语句在sql server 2005 中是可以执行的,在HQL中就有点问题啦,关键是怎么改。本人刚学SSH不久,对有些东西还希望各位多提一些建议 [b]问题补充:[/b] 我用的是Spring + Struts2 + Hibernate3.2架构的,所以 String hql = "from Goods order by PutTime desc"; List results = session.createQuery(hql).setMaxResults(5).list(); 用session好像不行啊
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页