关于 hiberate setMaxResults() 的问题
为什么我用setMaxResults()实现分页 只要设置值超过了10 调用list 只能查询10条数据;
设置少于10 ,正常
2012-04-13 10:58:30 INFO [PersonDAO,204] - 开始位置:0
Hibernate: select * from ( select person0_.PERSONID as PERSONID4_, person0_.BIRTHDAY as BIRTHDAY4_, person0_.PNAME as PNAME4_, person0_.ADRESS as ADRESS4_, person0_.TEL as TEL4_, person0_.EMAIL as EMAIL4_, person0_.MSN as MSN4_ from SCOTT.PERSON person0_ ) where rownum <= ?
Hibernate: select passport0_.PASSPORTID as PASSPORTID3_0_, passport0_.SERIAL as SERIAL3_0_, passport0_.EXPIRY as EXPIRY3_0_ from SCOTT.PASSPORT passport0_ where passport0_.PASSPORTID=?
Hibernate: select passport0_.PASSPORTID as PASSPORTID3_0_, passport0_.SERIAL as SERIAL3_0_, passport0_.EXPIRY as EXPIRY3_0_ from SCOTT.PASSPORT passport0_ where passport0_.PASSPORTID=?
Hibernate: select passport0_.PASSPORTID as PASSPORTID3_0_, passport0_.SERIAL as SERIAL3_0_, passport0_.EXPIRY as EXPIRY3_0_ from SCOTT.PASSPORT passport0_ where passport0_.PASSPORTID=?
Hibernate: select passport0_.PASSPORTID as PASSPORTID3_0_, passport0_.SERIAL as SERIAL3_0_, passport0_.EXPIRY as EXPIRY3_0_ from SCOTT.PASSPORT passport0_ where passport0_.PASSPORTID=?
Hibernate: select passport0_.PASSPORTID as PASSPORTID3_0_, passport0_.SERIAL as SERIAL3_0_, passport0_.EXPIRY as EXPIRY3_0_ from SCOTT.PASSPORT passport0_ where passport0_.PASSPORTID=?
Hibernate: select passport0_.PASSPORTID as PASSPORTID3_0_, passport0_.SERIAL as SERIAL3_0_, passport0_.EXPIRY as EXPIRY3_0_ from SCOTT.PASSPORT passport0_ where passport0_.PASSPORTID=?
Hibernate: select passport0_.PASSPORTID as PASSPORTID3_0_, passport0_.SERIAL as SERIAL3_0_, passport0_.EXPIRY as EXPIRY3_0_ from SCOTT.PASSPORT passport0_ where passport0_.PASSPORTID=?
Hibernate: select passport0_.PASSPORTID as PASSPORTID3_0_, passport0_.SERIAL as SERIAL3_0_, passport0_.EXPIRY as EXPIRY3_0_ from SCOTT.PASSPORT passport0_ where passport0_.PASSPORTID=?
Hibernate: select passport0_.PASSPORTID as PASSPORTID3_0_, passport0_.SERIAL as SERIAL3_0_, passport0_.EXPIRY as EXPIRY3_0_ from SCOTT.PASSPORT passport0_ where passport0_.PASSPORTID=?
Hibernate: select passport0_.PASSPORTID as PASSPORTID3_0_, passport0_.SERIAL as SERIAL3_0_, passport0_.EXPIRY as EXPIRY3_0_ from SCOTT.PASSPORT passport0_ where passport0_.PASSPORTID=?
2012-04-13 10:58:30 INFO [PersonDAO,209] - page list:10
2012-04-13 10:58:30 INFO [PersonAction,74] - personPage 信息:Page [pageCount=2, pageNo=1, pageSize=11, totalCount=20]
代码:
Session session=getSession();
Query query=session.createQuery("from Person");
Query query1=session.createQuery("select count(*) from Person");
try{
Page page=new Page(pageNo,PAGESIZE,(Long)query1.uniqueResult());
int first=(page.getPageNo()-1)*PAGESIZE;
log.info("开始位置:"+first);
query.setFirstResult(first);
query.setMaxResults(PAGESIZE);
List list=query.list();
page.setList(list);
log.info("page list:"+list.size());
return page;
}catch(RuntimeException re){
log.error("find all failed",re);
throw re;
}finally{
session.close();
}
[解决办法]
没有出现过你这种情况。