2011-11-17 49 views
1

如果我沒有設置setFirstResult(-)並遞歸調用criteria.setmaxresults(10)每一次,它會自動從數據庫中抓取下10個項目嗎?休眠setMaxResults分頁

+0

所以確認需要使用setFirstResult (偏移量)做分頁? – cometta

+0

是的,確認:-) –

回答

4

號必須使用criteria.setFirstResult(0)和頁面通過自己,這樣的事情:

public List getCarters(final int firstResult, final int maxResults) { 

    final Criteria criteria = sessionFactory.getCurrentSession() 
         .createCriteria(SomePersistentClass.class); 
         .add(Restrictions.eq("name", "Carter")) 
    criteria.setFirstResult(firstResult); 
    criteria.setMaxResults(maxResults); 

    return criteria.list(); 
} 
+0

+1爲Michael Caine參考。 –

+0

有沒有辦法使用Long來獲得最大結果? – chefarov

2

當然,沒有。標準從數據庫中抓取數據,只有當你調用.LIST()或.uniqueResult()

+0

是的,當然返回.list()..所以如果我用setmaxresult(10)調用相同的方法,它不會自動抓住接下來的10個項目?必須使用setfirstresult(offset)? – cometta

+0

如果你想要下10個項目,你應該使用setFirstResult(offset)和setMaxResults(10)。 –