firefoxeye 2009-09-15 15:38
浏览 495
已采纳

hibernate 分页查询语句问题

我的一个语句在使用Oracle时正常运行,换成MySQL后不能正常使用了[code="java"]String hql = "from News n where n.entry.entryid in ("
+ "select e.entryid from Entry e where e.parentid=1 ) "
+ "or n.entry.entryid=1 order by n.lastmodifydate desc";
List tempList = basicMan.pagedQuery(hql, 1, 2);[/code][code="java"]public List pagedQuery(final String hql, int currentPage, final int pageSize) {

    if (currentPage == 0) {
        currentPage = 1;
    }
    final int curPage = currentPage;
    List list = getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session)
                throws HibernateException, SQLException {

            Query query = session.createQuery(hql);
            List result = query.setFirstResult((curPage - 1) * pageSize)
                    .setMaxResults(pageSize).list();
            return result;
        }
    });
    return list;
}[/code]

错误代码如下[code="java"]Hibernate: select * from ( select news0_.NEWSID as NEWSID2_, news0_.ENTRYID as ENTRYID2_, news0_.NEWSTITLE as NEWSTITLE2_, news0_.CONTENT as CONTENT2_, news0_.KEYWORDS as KEYWORDS2_, news0_.LASTMODIFYDATE as LASTMODI6_2_, news0_.ORDERINDEX as ORDERINDEX2_, news0_.PICPATH as PICPATH2_ from zkq1f1_db.news news0_ where news0_.ENTRYID in (select entry1_.ENTRYID from zkq1f1_db.entry entry1_ where entry1_.PARENTID=1) or news0_.ENTRYID=1 order by news0_.LASTMODIFYDATE ) where rownum <= ?
Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:615)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:343)
at com.kcheng.dao.BasicDImp.pagedQuery(BasicDImp.java:169)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy5.pagedQuery(Unknown Source)
at test.my.dao.TestDao.main(TestDao.java:38)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query[/code][code="java"]Caused by: java.sql.SQLException: Every derived table must have its own alias[/code]

在不使用分页的情况下页正常运行

怎么没有人来帮助呢,提示一下也好啊
[b]问题补充:[/b]
query.setFirstResult((curPage - 1) * pageSize)

.setMaxResults(pageSize).list();
hibernate这种方式也一样啊
[b]问题补充:[/b]
确实问题出在[code="java"]query.setFirstResult((curPage - 1) * pageSize)

.setMaxResults(pageSize)[/code]
但是这是为什么呢?应该怎么改呢
[b]问题补充:[/b]
产生的sql语句是rownum <= ? 需要改成limit 但是hql该怎么改呢
[b]问题补充:[/b]
照你那样改的话代码改动蛮大,现在是因为转移数据库的时候出了问题,就是说代码不想去做大的改动了

  • 写回答

5条回答 默认 最新

  • iteye_18964 2009-09-15 17:01
    关注

    楼主oracle改成mysql数据库,hibernate.dialect,class映射generator类型改过了没有?
    mysql :
    hibernate.dialect:org.hibernate.dialect.MySQLInnoDBDialect
    generator:native

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低