-->
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 can I count the number of query results without actually
PostPosted: Sat May 21, 2005 10:37 pm 
Beginner
Beginner

Joined: Fri Apr 15, 2005 4:53 pm
Posts: 29
How can I count the number of query results without actually returning them?


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

thats from hibernate faq...but session doesnt have any iterate method :-s



thx


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 22, 2005 11:02 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
You can use the Criteria API

Code:
session.createCriteria(MyObject.class).setProjection(Projections.count("fieldToCount")).uniqueResult();

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 23, 2005 6:27 pm 
Beginner
Beginner

Joined: Fri Apr 15, 2005 4:53 pm
Posts: 29
vgiguere wrote:
You can use the Criteria API

Code:
session.createCriteria(MyObject.class).setProjection(Projections.count("fieldToCount")).uniqueResult();


What object instance is returned from the following statement:

session.createSQLQuery ("select count(*) from users").list().get(0);

Its not letting me typecast it to Integer or a String.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 1:48 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Debug and you will see :-).

But you should be able to cast it to Integer or at least a Number.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 7:39 pm 
Beginner
Beginner

Joined: Fri Apr 15, 2005 4:53 pm
Posts: 29
alesj wrote:
Debug and you will see :-).

But you should be able to cast it to Integer or at least a Number.


I've tried debugging but all it says is "java.lang.Object"

*confused*


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 3:24 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
How could you run that sql query without setting entity or scalar?

>> org.hibernate.QueryException: addEntity() or addScalar() must be called on a sql query before executing the query. [select count(*) from users]

Your sql should probably look something like this:
SELECT {person}.NAME AS {person.name}, {person}.AGE AS {person.age}, {person}.SEX AS {person.sex}
FROM PERSON {person} WHERE {person}.NAME LIKE 'Hiber%'


vgiguere's solution should definitely work.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 3:16 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
If you debug it, you will see that it is an Integer.

So, you can do:
Code:
Query query = session.createQuery("select count(*) from whatever");
Integer myAnswer = (Integer) query.uniqueResult();


You should use uniqueResult() if you are using an aggregation function. Otherwise, if you like using .list()just get the first result of the returned collection. It will still be an Integer.

_________________
Vincent Giguère
J2EE Developer


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.