[分享]mysql 使用 limit (方法:setMaxResults )时出错_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2305 | 回复: 0   主题: [分享]mysql 使用 limit (方法:setMaxResults )时出错        下一篇 
napolenAx
注册用户
等级:少校
经验:802
发帖:118
精华:1
注册:2011-8-30
状态:离线
发送短消息息给napolenAx 加好友    发送短消息息给napolenAx 发消息
发表于: IP:您无权察看 2014-12-1 15:01:14 | [全部帖] [楼主帖] 楼主

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 并安装后,就没有错误了。

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论