hibernate query无法分页
              List all = null;
    String hql = "from FzInfoTrade as f where f.tradeTitle like ?";
    Query q = super.getSession().createQuery(hql);

[color=red] q.setFirstResult((currentPage - 1) * lineSize);
q.setMaxResults(lineSize);[/color]
q.setString(0, "%" + cond + "%");
all = q.list();
return all;
遇到一个很奇怪的问题 上面的代码 加了红色的2句之后就有错
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:1596)
at org.hibernate.loader.Loader.list(Loader.java:1577)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:395)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:844)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at fz.daoimpl.InfoTradeDAOImpl.queryAll(InfoTradeDAOImpl.java:59)
at fz.daoimpl.InfoTradeDAOImpl.main(InfoTradeDAOImpl.java:68)
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 '1 fzinfotrad0_.trade_id as trade1_, fzinfotrad0_.trade_title as trade2_15_, fzin' at line 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1695)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3026)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1137)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1231)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:120)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1272)
at org.hibernate.loader.Loader.doQuery(Loader.java:391)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.doList(Loader.java:1593)
... 7 more

一直怀疑是否是环境问题 但去掉了就没事 这样的写法没有问题 不知道是哪里的原因?
[b]问题补充:[/b]
mysql的版本是5.2的 我也怀疑是版本问题 但是我这个mysql里又另一个数据库用另一个项目同样的方法是没出问题的
用hibernate好像不能直接在hql里写limit吧
肯定是哪里环境出问题 但就是找不到

7个回答

org.hibernate.dialect. MySQLDialect
hibernate方言问题

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 '1 fzinfotrad0_.trade_id as trade1_, fzinfotrad0_.trade_title as trade2_15_, fzin' at line 1

它是说你的sql语法写得有问题,mysql版本是多少的啊

语法错误.

http://topic.csdn.net/u/20080228/12/dabe64a3-2d52-4a13-b729-cfadf5105aa0.html
呵呵 请参考下这篇文章;mysql可能不支持hibernate的setFirstResult方法所使用的分页方式;所以你可以考虑修改sql语句,使用limit来完成分页的功能;

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 '1 fzinfotrad0_.trade_id as trade1_, fzinfotrad0_.trade_title as trade2_15_, fzin' at line 1
可能是mysql版本问题

用hibernate的确不能直接在hql里写limit

数据库版本不对

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问