-->
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: Paging, Criteria APIs and count(*)
PostPosted: Wed Sep 28, 2005 2:53 pm 
Newbie

Joined: Tue May 17, 2005 8:39 am
Posts: 14
Hi

I have written an application with search screens where I use Hibernate's 2.1 Criteria API to dynamically generate select queries. I use setFirstResult() and setMaxResult to implement paging of result set, because there can be a lot of records in database.

Now I need to get total number of records that satisfy given criteria. So I can display something like "x-y from total z records". Which would be the best way to get total number of records ? I have some ideas, for example:

1) If it's possible to get SQL statement generated by Hibernate (?) I could modify by replacing fields with "count (*)"

2) It's also possible to read all records from db and display just a subset, but it would have very bad performance.

Any ideas ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 3:25 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
or use criteria with projections

http://www.hibernate.org/hib_docs/v3/reference/en/html/querycriteria.html#querycriteria-projection


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 8:26 pm 
Newbie

Joined: Mon Sep 26, 2005 2:40 pm
Posts: 17
Location: Seattle
How can you add a projection, or otherwise get the count, but leave the original Criteria unchanged?

My page results object takes a DetachedCriteria in the constructor, but the getCount() query needs to add the count Projection. I can't seem to undo that addition.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 12:20 am 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
this is a workaround to leave the criteria unchanged for later query. This needs Scrollable result sets.

What you need to do is.

Code:
ScrollableResults sr = criteria.scroll()
sr.last();
int rowNumber = sr.getRowNumber() ;


rowNumber + 1 should be what you are looking for.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 7:08 pm 
Newbie

Joined: Mon Sep 26, 2005 2:40 pm
Posts: 17
Location: Seattle
Thanks, that worked great!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 1:32 pm 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
how about rating the post then


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 1:41 pm 
Newbie

Joined: Mon Sep 26, 2005 2:40 pm
Posts: 17
Location: Seattle
I'm not the OP, you'll have to ask MarisO about that.


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.