-->
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.  [ 4 posts ] 
Author Message
 Post subject: Criteria query and pagination
PostPosted: Fri Sep 26, 2003 12:34 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I want to use
criteria.setFirstResult(xxx);
criteria.setMaxResults(yyy);

to display list divided into pages. But how can I determine total number of pages?

I though it would be useful to have count() method in Criteria class - then paging becomes simple:

Criteria q = session.createCriteria(Department.class);
q.add( Expression.eq("xxx", xxx) );
// ...
int total = q.count();

int totalPages = total / pageNo;
if (pageNo > totalPages)
pageNo = totalPages;

int first = itemsPerPage * pageNo;

q.setFirstResult(first);
q.setMaxResults(itemsPerPage);

List list = q.list();


Top
 Profile  
 
 Post subject: Re: Criteria query and pagination
PostPosted: Wed Feb 08, 2006 9:03 am 
Beginner
Beginner

Joined: Mon Jan 30, 2006 2:28 am
Posts: 47
Location: INDIA
Try to get total count

Using
List list = q.list();
int count = list.size(); // To get Total Count
System.out.println("Total :"+count);

_________________
A.Edward Durai
"The things which are impossible with men are possible with God."


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 11:45 am 
Newbie

Joined: Tue Jul 26, 2005 4:32 pm
Posts: 8
Loading the entire list of objects to get count is extremely inefficient. What if you have a million rows? Your performance would be horrible.
In fact, I am using patched Hibernate version 2.1.5 which supplies
count method on Criteria.

If there another way to do it, I would love to know.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 1:34 pm 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
If you have scrollable resultsets. You can use the following.
Code:
ScrollableResults sr = criteria.scroll()
sr.last();
int rowNumber = sr.getRowNumber() ;


rowNumber + 1 should be what you are looking for.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.