简体   繁体   中英

Hibernate using Criteria setMaxResults giving SQLGrammarException

I have made criteria, but when I use setMaxResults, it will give me exception SQLGrammarException. But when I comment setMaxResults, it will work an return all entries after first result. I'm using MySQL database.

Code:

return criteria
            .setFirstResult(start)
            .setMaxResults(end)
            .setFetchSize(end)
            .list();

Exception:

org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2536)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
    at org.hibernate.loader.Loader.list(Loader.java:2271)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)

....

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

Problem is now solved. I had wrong dialect in hibernate properties. I had copied my properties from other project that used SQL Server, but this one uses MySQL, so wrong dialect was causing this error.

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM