-->
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.  [ 6 posts ] 
Author Message
 Post subject: Record count with named query
PostPosted: Wed Mar 15, 2006 7:18 pm 
Beginner
Beginner

Joined: Wed Feb 15, 2006 2:51 pm
Posts: 20
Hello,

Is there an easy way of counting records that where selected in a named query, before i get the results in a collection??

I want to count the number of records before I apply my paging, using
setFirstResult
setMaxResults



I know that I can do it with criteria, buy using
criteria.setProjection(Projections.rowCount());

thank you


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 8:09 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
I'd be interested to see a more "correct" solution, but i've implemented a little wrapper in my DAO that will run a

Code:
   querySt = "select count(*) from ("+namedQuery+")";


and then

Code:
  Integer count =(Integer) countQuery.list().get(0);


You dont get the precompiled goodness from that, and you're running the query twice... but it works just fine for how I need to use it.

I don't know of a better way to do it from a Query object.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 12:46 am 
Newbie

Joined: Mon Jun 06, 2005 10:11 am
Posts: 14
From hibernate documentation

You can count the number of query results without actually returning them:
( (Integer) session.iterate("select count(*) from ....").next() ).intValue()

You can find the size of a collection without initializing it:
( (Integer) session.iterate("select count(*) from ....").next() ).intValue();

/Ashwani


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 22, 2006 4:20 pm 
Newbie

Joined: Wed Nov 03, 2004 4:40 pm
Posts: 4
Is this example from the documentation a bit out of date? I don't see any iterate() method for a org.hibernate.Session for Hibernate3. There seems to be a deprecated method for Hibernate2 with the org.hibernate.classic.Session.

So for Hibernate3, I think the solution (and documentation) should read:

( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue()


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 22, 2006 4:29 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
Unless I'm mistaken,

Code:
Integer count =(Integer) countQuery.list().get(0);


does the same thing!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 12:07 am 
Newbie

Joined: Mon Jun 06, 2005 10:11 am
Posts: 14
yea, I also noticed this. The documentation does not seem to be updated as per hibernate 3.0



bolandb wrote:
Is this example from the documentation a bit out of date? I don't see any iterate() method for a org.hibernate.Session for Hibernate3. There seems to be a deprecated method for Hibernate2 with the org.hibernate.classic.Session.

So for Hibernate3, I think the solution (and documentation) should read:

( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue()


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