2010-11-30 78 views
1

我已經做了條件,但是當我使用setMaxResults時,它會給我異常SQLGrammarException。但是,當我評論setMaxResults時,它將在返回第一個結果後返回所有條目。我正在使用MySQL數據庫。Hibernate使用Criteria setMaxResults給出SQLGrammarException

代碼:

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

例外:

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

嘗試設置「show_sql`屬性爲true,並檢查了生成的查詢嘗試figureout從中 – 2010-11-30 07:27:49

回答

4

問題現在已經解決了。我在hibernate屬性中使用了錯誤的方言。我從其他使用SQL Server的項目複製了我的屬性,但是這個使用了MySQL,所以錯誤的方言導致了這個錯誤。

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect 
+0

問題,我當我使用這個屬性得到了錯誤:<屬性名=‘hibernate.dialect’值=「組織。 hibernate.dialect.HSQLDialect「/>,但是當我刪除它,它工作正常。謝謝新手:) – Hazim 2016-02-05 19:27:14

相關問題