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条)

报告相同问题?

悬赏问题

  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
  • ¥15 SQL Server analysis services 服务安装失败
  • ¥15 用html创建一个个人网页,提供网页页面
  • ¥20 java项目连接SqlServer数据库报错
  • ¥15 基于面向对象的图书馆借阅管理系统
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥20 centos linux 7.9安装php8.2.18不支持mysqli模块的问题
  • ¥15 stata空间计量LM检验
  • ¥15 关于k8s node节点被释放后如何驱逐节点并添加新节点
  • ¥15 subprocess.CalledProcessError: Command ‘[‘ninja‘, ‘-v‘]‘ returned non-zero exit status 1