Using Projections.rowCount() does not get the count in the same query, it issues a separate count query.
http://raibledesigns.com/wiki/Wiki.jsp?page=HibernatePagination On 6/18/07, j2ee dodo <[EMAIL PROTECTED]> wrote:
Hi gang, Just to make myself useful. The workaround for the common problem of using the same Criteria object to get both paged data and total results count of all pages through projection for pagination in Hibernate 3.2 seemed to be http://forum.hibernate.org/viewtopic.php?t=956564&highlight=pagination Basically, the magic bit is public Page(Criteria criteria , String noPage, int pageSize) throws HibernateException { this.page = new Integer(noPage).intValue(); this.pageSize = pageSize; int initialRow = page * pageSize; int maxNbRows = pageSize + 1; // +1 pour savoir si il y a un suivant criteria.setFirstResult( initialRow ); criteria.setMaxResults(maxNbRows); *results = criteria.list();* criteria.setFirstResult(0); criteria.setProjection(Projections.rowCount()); * Integer result = (Integer) countCriteria.uniqueResult(); * *this.totalResults = result.intValue(); * } haven't tried this out myself, so can't say if it's efficient (e.g. using the same SELECT to retrieve both data and row count) or even work. Is that the best practice to get total results count? One other thing I am keen to know is if it's good practice or even practical to cache criteria queries (e.g. Criteria.setCachable(true); Criteria.setCacheMode(...); ) in pagination (Ok, I really shud ask Hibernate ppl this but can't blame me for taking a shot here ...) Thanks, Sam, ----- Original Message ----- *From:* Sanjiv Jivan <[EMAIL PROTECTED]> *To:* [email protected] *Sent:* Monday, June 18, 2007 10:58 PM *Subject:* Re: [appfuse-user] hibernate pagination question That seems incorrect. There is a pattern where one gets an extra record only to determine if we're on the last page of data but then return pageSize records to the user, but this doesn't seems to be the case in the wiki article. See http://www.jroller.com/page/sjivan?entry=hibernate_and_oracle_pagination_gotchafor details on the Hibernate pagination pattern. On 6/17/07, j2ee dodo <[EMAIL PROTECTED]> wrote: > > Hi gang, > > According to wiki on http://raibledesigns.com/wiki/Wiki.jsp?page=DisplayTagAndHibernatePagination > > > > *public *List getTestData(*int *page, *int *pageSize){ > > Query query = getSession ().createQuery("from Test"); > *return *query.setFirstResult(page * pageSize ).setMaxResults(* > pageSize+1*).list (); > } > > We have pagSize+1 above, but I can't figure out why we really need > pageSize+1. So can > someone enlighten me on this? > > Thanks, > > Sam >
