-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Criteria, Pagination and Number of Rows Available (Max Pgs)
PostPosted: Fri May 06, 2005 1:17 pm 
Regular
Regular

Joined: Sun Sep 26, 2004 9:27 pm
Posts: 75
Location: Atlanta, GA, USA
Is there a quick and multiple-platform solution for Hibernate to determine the number of rows that would have been retrieved if a LIMIT clause was not utilized in a query?

In other words, to fully implement pagination, we need three things:
  1. The number of objects to display per page,
  2. The current page for display, and
  3. The total number of pages possible.

The first two items may be delivered using ScrollableResult.setRowNumber(itemsPerPage * currentPage) then looping across itemsPerPage number elements creating a list.

Would ScrollableResult.last(), ScrollableResult.getRowNumber() be too taxing on the system? Or is this an appropriate solution?

In essence, I need the MySQL equivalent of SQL_CALC_FOUND_ROWS for any given query, produced magically by Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 06, 2005 3:43 pm 
Regular
Regular

Joined: Sun Sep 26, 2004 9:27 pm
Posts: 75
Location: Atlanta, GA, USA
Since this isn't supported by default, I started doing some testing, and my suggestion isn't acceptable.

Code:
      Session session = SpringSupport.getSession() ;
      long startTime = System.currentTimeMillis() ;
      mLogger.warn(Float.toString(this.secondsLater(startTime))) ;
      Criteria criteria = session.createCriteria(MyCustomObject.class) ;
      criteria.setFetchSize(25) ;
      mLogger.warn(Float.toString(this.secondsLater(startTime))) ;
     
      ScrollableResults results = criteria.scroll() ;
      mLogger.warn(Float.toString(this.secondsLater(startTime))) ;
      // Move to the end
      results.last() ;
      mLogger.warn(Float.toString(this.secondsLater(startTime))) ;
      int lastRow = results.getRowNumber() ;
      mLogger.warn(Float.toString(this.secondsLater(startTime))) ;
     
      mLogger.debug("Last row is " + lastRow) ;


Produces the following output:

Code:
15:18:47 [main] WARN  p2p.dao.HibernatePaginationTest.testScrollableTime(HibernatePaginationTest.java:28) 0.0
15:18:47 [main] WARN  p2p.dao.HibernatePaginationTest.testScrollableTime(HibernatePaginationTest.java:31) 0.016
15:18:52 [main] WARN  p2p.dao.HibernatePaginationTest.testScrollableTime(HibernatePaginationTest.java:34) 4.985
15:18:52 [main] WARN  p2p.dao.HibernatePaginationTest.testScrollableTime(HibernatePaginationTest.java:37) 5.0
15:18:52 [main] WARN  p2p.dao.HibernatePaginationTest.testScrollableTime(HibernatePaginationTest.java:39) 5.016
15:18:52 [main] DEBUG p2p.dao.HibernatePaginationTest.testScrollableTime(HibernatePaginationTest.java:41) Last row is 589823


So that's a 5 second wait for the scroll() call to return.

My good friend Tim Collins has a very elegant solution, that is database independant. He suggests executing the query twice once for the data set, and a second time only returning count(*) for the number of rows.

I'm going to attempt to show that I'm just as smart as Tim Collins, and try to incorporate this into a cached Criteria, or a CountProjection if I can figure out what the hell it does, due to the lack of Javadocs.

I'll post results here.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.