一条HQL语句引起的异常

表结构:
CREATE TABLE IF NOT EXISTS tb_cases(
id int(11) NOT NULL auto_increment,
cdate date default NULL,

title varchar(50) default NULL,

cont text,

imgpath varchar(80) default NULL,

apply text,

PRIMARY KEY (id)

)ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Hibernate映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">























HQL语句:
String FIND_CASENUM = " from Cases order by cdate desc ";

实现方法:
public List getCasesNum(final int showPage, final int pageSize){
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(final Session session) {
Query query = session.createQuery(FIND_CASENUM);
query.setFirstResult((showPage - 1) * pageSize);
query.setMaxResults(pageSize);
List casesList = query.list();
if(null == casesList || casesList.isEmpty()){
casesList = null;
}
return casesList;
}
});
}

抛出异常:
Hibernate: select top 4 cases0_.id as id1_, cases0_.cdate as cdate1_, cases0_.title as title1_, cases0_.cont as cont1_, cases0_.imgpath as imgpath1_, cases0_.apply as apply1_ from tb_cases cases0_ order by cases0_.cdate desc
2009-07-08 13:06:22,281 [org.hibernate.util.JDBCExceptionReporter]-[WARN] SQL Error: 1064, SQLState: 42000
2009-07-08 13:06:22,281 [org.hibernate.util.JDBCExceptionReporter]-[ERROR] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '4 cases0_.id as id1_, cases0_.cdate as cdate1_, cases0_.title as title1_, cases0' at line 1
2009-07-08 13:06:26,812 [org.apache.struts.action.RequestProcessor]-[WARN] Unhandled Exception thrown: class org.springframework.dao.InvalidDataAccessResourceUsageException
2009-07-08 13:06:26,812 [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/bianyaqi].[action]]-[ERROR] Servlet.service() for servlet action threw exception
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at com.base.hibernate.dao.impl.CasesDaoImpl$1.doInHibernate(CasesDaoImpl.java:65)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:342)
at com.base.hibernate.dao.impl.CasesDaoImpl.getCasesNum(CasesDaoImpl.java:60)
at com.base.spring.service.impl.CasesServiceImpl.getCasesNum(CasesServiceImpl.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy1.getCasesNum(Unknown Source)
at com.base.struts.action.IndexAction.execute(IndexAction.java:130)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.base.common.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:123)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.base.common.filter.ForceNoCacheFilter.doFilter(ForceNoCacheFilter.java:36)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '4 cases0_.id as id1_, cases0_.cdate as cdate1_, cases0_.title as title1_, cases0' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1885)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
at org.hibernate.loader.Loader.doQuery(Loader.java:674)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
... 54 more

怀疑是HQL语句有什么错误,但愣是没找出来
映射文件与表中的字段名也检查过了,没什么问题啊!
查看了好久也没找出到底什么地方的错误
还请各们朋友们帮下忙看下到底怎么回事!

补充:
如果将HQL语句改成 from Product 问题依旧
但在Mysql数据库中使用 select id,cdate,title,cont,imgpath,apply from tb_cases;
语句没有任何错误!

7个回答

后台报SQL Error: 1064, SQLState: 42000错误
出现种情况一般是数据库中的表字段命名含有:关键字和保留字

你查出的数据是不是太大了,内存不够吧

MYsql 不支持TOP查询,你的方言设置错了

你改一下你的方言设置

HIBERNATE SQL的方言设置:

http://arron-li.iteye.com/blog/290700

MySQL: org.hibernate.dialect.MySQL5Dialect(5.0以上版本);org.hibernate.dialect.MySQLDialect(5.0以下版本)

You have an error in your SQL syntax

这个错误很明显就是sql的语法写得有问题导致 ,

select top 4 ,这个好像是sqlserver的语法吧,

是不是你的数据库方言不是配置的是mysql

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