An error occurred while using limit (method: setmaxresults) in MySQL.

Source: Internet
Author: User

An error occurred while using limit for MySQL.

Platform: Eclipse platform: Version 3.0.1, Hibernate synchronizer 2.3.1, MySQL 4.1.9, Win2k
Database Table:
Create Table Department (
Id integer not null auto_increment,
Name varchar (255) not null,
Primary Key (ID ));

Use hibernate synchronizer to generate departmentdao. Java and add the find (INT, INT) method. The method code is as follows:

Public java. util. List find (INT firstresult, int maxresults) throws hibernateexception {
Criteria = createcriteria ();
// Method 1:
Criteria. setfirstresult (firstresult );
Criteria = criteria. setmaxresults (maxresults );
// Method 2:
/*
Criteria. Add (expression. SQL ("Limit ?, ? ",
New integer [] {New INTEGER (firstresult), new INTEGER (maxresults )},
New Type [] {New integertype (), new integertype ()}));
*/
Return criteria. List ();

}

This code is mainly used for pagination of the content list, but I found that I only need to use the setmaxresults method of criteria, but there is no error if I do not use this method or do not use limit, the generated hql code is as follows:

Hibernate: select this. ID as id0 _, this. Name as name0 _ from department this where 1 = 1 limit ?, ?

That is to say, if the statement contains the limit keyword, an error is reported. The error message is as follows:

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. doclist (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

After a few days of failure, I had to post a post on the www.hibernate.org.cn forum and get the advice from a netizen named "Hello pig. The original JDBC version I used was 3.1.6. After I downloaded 3.1.7 from the Internet and installed it, there was no error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.