org.hibernate.hql.ast.QuerySyntaxException:unexpe

以下是我在开发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 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 findTopByPubTime(){
List 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 listTopTime = topPubTime.findTopByPubTime();
session.setAttribute("topTime",listTopTime);//将手机信息保存到session中
%>

   <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好像不行啊

6个回答

[quote]我用的是Spring + Struts2 + Hibernate3.2架构的,所以
String hql = "from Goods order by PutTime desc";
List results = session.createQuery(hql).setMaxResults(5).list();
用session好像不行啊 [/quote]

LZ可以试试下面的办法
[code="java"]Session session = this.getHibernateTemplate().getSessionFactory().openSession();
String hql = "from Goods order by PutTime desc";

Transaction tr = session.beginTransaction();
tr.begin();
List results = session.createQuery(hql).setMaxResults(5).list();
tr.commit();[/code]

晕,你的这条sql写得语法有问题
select top 5 * from com.no9.entity.Goods order by PutTime desc

直接放到数据库去执行看能不能成功,一定有问题

HQL语法出错了,LZ好好检查下自己的HQL

你的HQL出现问题了
你需要查询前几条数据,我给出这样支持任何数据库,不管是mysql还是mssql都支持。如果你用了top在mssql是可以的,但是mysql是不支持的。所以我建议你用我以下给出的方案。
String hql = "from com.no9.entity.Goods order by PutTime desc ";
List result = session.createQuery(hql)
.setFirstResult(0).setMaxResults(5).list();
return result;

[code="java"]String hql = "from Goods order by PutTime desc";
List results = session.createQuery(hql).setMaxResults(5).list();[/code]

LZ,这个results就是查询出结果的前5条记录

很明显语法错误

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐