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 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿