我的一个语句在使用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]
照你那样改的话代码改动蛮大,现在是因为转移数据库的时候出了问题,就是说代码不想去做大的改动了