-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to get total page count for pagination using Criteria
PostPosted: Thu May 13, 2010 12:27 pm 
Newbie

Joined: Thu May 13, 2010 12:21 pm
Posts: 7
Hello All,

I am new to Hibernate and to this forum! :-) I need your help to figure out best way to calculate total pages for pagination I am implementing in my UI. I am using Criteria class and using .setFirstResult and .setMaxResults to get only specific page information. Like, for first page if my page size is 3 I set the .setFirstResult to 0 and .setMaxResults to 3. My problem is, for example, if in my database I have 30 records, when I do this way my result list size is also giving back 3.

Is there any way in Criteria I can use .setFirstResult and .setMaxResults but at the same time get a total count? What would be the best way to get total count for pagination if I use .setFirstResult and .setMaxResults?

I will appreciate any help or suggestion here.

Thanks!


Top
 Profile  
 
 Post subject: Re: How to get total page count for pagination using Criteria
PostPosted: Thu May 13, 2010 1:11 pm 
Newbie

Joined: Mon May 10, 2010 1:41 pm
Posts: 15
Code has not been tested:

int count = 0;
int maxRecordsPerPage = 30;
List<cat> list = null;

void someMethod(){
Criteria cats = session.createCriteria(cat.class);
cats.setFirstResult(start);
cats.setMaxResults(maxSize);
list = cats.list();
count = list.size();
}

int getPageCount(){
int fullyFilledPages = count / maxRecordsPerPage;
int isPartialPage = ((count % maxRecordsPerPage) > 0)?(1):(0);
return fullyFilledPages + isPartialPage;
} //of course I would make the above one statement but then I like dense
//you could also use float for the number of pages and then use a ceiling function to round all cases up.


Top
 Profile  
 
 Post subject: Re: How to get total page count for pagination using Criteria
PostPosted: Thu May 13, 2010 1:43 pm 
Newbie

Joined: Thu May 13, 2010 12:21 pm
Posts: 7
Thank you for your reply. I think I should have been more precise in my question.

What I am trying to ask is -if I have 30 records in the table but my page size is 3 records at a time, is there any way to both restrict fetch size to 3 but get total count of 30 using Criteria? Because, if I set maxResults to 3 then my count also will be 3, which is incorrect. And if I set mexResults to 30 then my query will always retrieve 30 records even if I need only 3 at a time.

Is there anyway I can get total count for the query (in this case 30) but fetch only the number I am looking for (3)?

Thanks,


Top
 Profile  
 
 Post subject: Re: How to get total page count for pagination using Criteria
PostPosted: Thu May 13, 2010 3:29 pm 
Newbie

Joined: Thu May 13, 2010 12:21 pm
Posts: 7
any suggestion please?


Top
 Profile  
 
 Post subject: Re: How to get total page count for pagination using Criteria
PostPosted: Thu May 13, 2010 4:14 pm 
Newbie

Joined: Thu May 13, 2010 12:21 pm
Posts: 7
OK..I got it. This is what I did -

Code:

Integer totalSize = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
criteria.setResultTransformer(Criteria.ROOT_ENTITY);



Top
 Profile  
 
 Post subject: Re: How to get total page count for pagination using Criteria
PostPosted: Fri May 14, 2010 10:29 am 
Newbie

Joined: Wed Mar 03, 2010 5:51 am
Posts: 4
int count=((Integer) c.setProjection(Projections.rowCount()).uniqueResult()).intValue();
int index=1;
int noofRcordsPerPage=3; //it can be any number u can update according to ur requirements


String action=ae.getActionCommand()
Criteria crit=null;

if(action.equalsIgnoreCase("next")){ //logic for next page
index=index+1;
crit =session.createCriteria(User.class)
.addOrder( Order.asc("fname") )
.setFirstResult((index*noofRcordsPerPage)-noofRcordsPerPage)
.setMaxResults(noofRcordsPerPage);
}else{
index=index-1; //logic for previous page
crit = session.createCriteria(User.class)
.addOrder( Order.asc("fname") )
.setFirstResult((index*noofRcordsPerPage)-noofRcordsPerPage)
.setMaxResults(noofRcordsPerPage);
}


page count is simple (no of records / noofpages per page) 30/3 =10
u can have no of pages =10
take n=10



code is tested
working fine
hope it willl help

Thanks

_________________
dsvk


Top
 Profile  
 
 Post subject: Re: How to get total page count for pagination using Criteria
PostPosted: Mon Oct 20, 2014 5:18 pm 
Newbie

Joined: Mon Oct 20, 2014 5:16 pm
Posts: 1
when I run your code, it shows error:
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
at com.tpgsi.framework.services.SearchService.getSearchResult(SearchService.java:52)


how can you say you tested code. lair.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.