mysql 使用 limit 时出错
平台:Eclipse 平台:版本:3.0.1,Hibernate Synchronizer 2.3.1,mysql 4.1.9,win2k
数据库表:
CREATE TABLE department (
id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
PRIMARY KEY (id));
通过 Hibernate Synchronizer 生成 DepartmentDAO.java 并增加方法 find(int,int), 方法代码如下:
public java.util.List find(int firstResult, int maxResults) throws HibernateException {
Criteria criteria = createCriteria();
//第一种方法:
criteria.setFirstResult(firstResult);
criteria = criteria.setMaxResults(maxResults);
//第二种方法:
/*
criteria.add(Expression.sql("limit ?, ?",
new Integer[]{new Integer(firstResult), new Integer(maxResults)},
new Type[]{new IntegerType(), new IntegerType()}));
*/
return criteria.list();
}
这段代码主要用于内容列表分页,但我发现,我只要使用 Criteria 的 setMaxResults 方法时就出错,而没有这个方法或不使用 limit 时就没有错误,生成的 HQL 代码如下:
Hibernate: select this.id as id0_, this.name as name0_ from department this where 1=1 limit ?, ?
也就是说,当语句中存在:limit 关键字时,就会报错,报错内容如下:
testFindintint(com.linqi.test.dao.DepartmentDAOTest)
net.sf.hibernate.exception.SQLGrammarException: Unable to perform find
at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:69)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4131)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3663)
at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:238)
at com.linqi.test.dao.DepartmentDAO.find(DepartmentDAO.java:26)
at com.linqi.test.dao.DepartmentDAOTest.testFindintint(DepartmentDAOTest.java:63)
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:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
Caused by: java.sql.SQLException: 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 '?, ?' at line 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2851)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1534)
at com.mysql.jdbc.ServerPreparedStatement.serverPrepare(ServerPreparedStatement.java:1485)
at com.mysql.jdbc.ServerPreparedStatement.(ServerPreparedStatement.java:151)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1309)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1281)
at net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:263)
at net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:236)
at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:67)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:784)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
at net.sf.hibernate.loader.CriteriaLoader.list(CriteriaLoader.java:118)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3660)
... 18 more
整了几天都没有搞好,不得已的情况下,到 www.hibernate.org.cn 论坛发贴后,得到名为“好猪一只”的网友指点后才知道是 jdbc 的问题。我原来使用的 JDBC 版本是 3.1.6,到网上下载了 3.1.7 并安装后,就没有错误了。
--转自