chengaezakmi
chengaezakmi
采纳率71.4%
2017-12-14 17:15

hibernate使用query.setMaxResults()和query.setFirstResult()进行对mysql的数据进行分页查询

5
已采纳

我在使用hibernate进行对mysql的数据进行分页查询的时候,用到执行到query.setMaxResults()的时候,报错误
图片说明

报错:
Hibernate: select top 4 goods0_.id as id5_, goods0_.supplierId as supplierId5_, goods0_.bigCategoryId as bigCateg3_5_, goods0_.smallCategoryId as smallCat4_5_, goods0_.goodsBianhao as goodsBia5_5_, goods0_.name as name5_, goods0_.synopsis as synopsis5_, goods0_.unitprice as unitprice5_, goods0_.brand as brand5_, goods0_.factoryProduction as factory10_5_, goods0_.purchasePrice as purchas11_5_, goods0_.chanpinxilie as chanpin12_5_, goods0_.chanpinleixing as chanpin13_5_, goods0_.gonglv as gonglv5_, goods0_.guigechicun as guigech15_5_, goods0_.yingyongkongjian as yingyon16_5_, goods0_.shiyongmianji as shiyong17_5_, goods0_.chanpinfengge as chanpin18_5_, goods0_.chanpingongneng as chanpin19_5_, goods0_.sewen as sewen5_, goods0_.xiansezhishu as xiansez21_5_, goods0_.caizhi as caizhi5_, goods0_.zhibao as zhibao5_ from ruilinxitong.goods goods0_ where 1=1 and goods0_.smallCategoryId=?
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2235)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
at org.hibernate.loader.Loader.list(Loader.java:2124)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at com.ruilin.dao.impl.GoodsDaoImpl.getAlumByGidAndTitle(GoodsDaoImpl.java:54)
at com.ruilin.dao.impl.GoodsDaoImpl.main(GoodsDaoImpl.java:86)
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 '4 goods0_.id as id5_, goods0_.supplierId as supplierId5_, goods0_.bigCategoryId ' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1812)
at org.hibernate.loader.Loader.doQuery(Loader.java:697)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2232)
... 9 more

现在该如何解决呀

很急的问题,帮忙解答,万分感谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

7条回答

  • zhangcc233 伏月又九 4年前

    原因可能有以下几个:
    1、你的hql语句、参数设置有误(从hql段段拼接,参数的先不传递可以解决这个问题)
    2、有可能是你的hibernate配置有误(如果其他的hql能执行则可排除)

    出现这种原因,大部分是hql问题,你多测试几次,hql一段一段执行,参数也可以从hql执行无误之后再传入来解决

    点赞 评论 复制链接分享
  • yujiaao COCO_AS 4年前

    and g_name like:g_name

    like 后面应该有空格才对

    点赞 1 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 4年前

    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 '4 goods0_.id as id5_, goods0_.supplierId as supplierId5_, goods0_.bigCategoryId ' at line 1

    检查这里提到的字段是否拼写都是正确的。

    点赞 1 评论 复制链接分享
  • chengaezakmi chengaezakmi 4年前

    已经解决,原来是hibernate配置问价中的sql方言指定错了,没有指定到mysql的sql方言。谢谢各位指导!

    点赞 评论 复制链接分享
  • chengaezakmi chengaezakmi 4年前

    hql语句是没有问题的,因为我把query.setMaxResults()和query.setFirstResult()这两个注释掉运行的时候,是完全没有问题的,加上这两句后就报错了。之前用sql server数据库的时候是可以的,现在换了mysql数据库之后就出现了这个问题。

    点赞 评论 复制链接分享
  • qq_32709167 IT发ET烧了 4年前

    有个办法是你可以在控制台复制sql语句(show_sql为true),然后粘贴在数据库中查询看有没有报错,就可以知道是不是sql语句问题

    点赞 评论 复制链接分享
  • Mr_Huang_ning Mr_Huang_ning 4年前

    like后面要有空格

    点赞 评论 复制链接分享