-->
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 get total record count
PostPosted: Fri Sep 08, 2006 7:03 pm 
Newbie

Joined: Mon Jul 31, 2006 2:41 pm
Posts: 13
I'm using Criteria for a report with lots of filters, and page flipping. For the page flipping I'm using the:

criteria.setMaxResults();
criteria.setFirstResult();


I would like to be able to display the total records for the user, however this list will just have the 25 because I'm only doing 25 per page. Is there somehow to count the total records? something like:

criteria.getTotalRecords()

I've seen some people do a criteria.list().size(), but that is horrible and I won't do that.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 08, 2006 7:15 pm 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
If you are using Hibernate 3:

create another Criteria with the same conditions, and

Code:
criteria.setProjection(Projections.rowCount());
Integer result = (Integer) criteria.uniqueResult();


-----------
don't forget credits[/code]

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 08, 2006 7:26 pm 
Newbie

Joined: Thu May 12, 2005 5:54 pm
Posts: 16


We have queries like this:

Code:
select title.name, title.descr, schedule.date
from title, schedule
where.....


We have a queryCriteria on which we set:
Code:
setFirstResult(n1)
setMaxResults(n2)


On queryCriteria we have projections for title.name,title.descr,
schedule.date

To get total count we need to do the following:

Code:
select count (distinct title.name || title.descr || schedule.date)


Since there is no concatenation in Projections we have created our own classes to do that.Refer to this post.
http://forum.hibernate.org/viewtopic.php?t=964506


Thus, code for above looks like this:

Code:
propertyNames = "title.name;title.descr;schedule.date";
countCriteria.setProjection(ExtraProjections.countMultipleDistinct(propertyNames))


Note queryCriteria and countCriteria are exactly the same in terms of query. Only in terms of projections they differ. They are different object instances.

Hope this helps.

Cheers.



Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 08, 2006 10:12 pm 
Newbie

Joined: Mon Jul 31, 2006 2:41 pm
Posts: 13
Thank you for the quick responses. This helps alot.


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.