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: How to check whether a query has results
PostPosted: Wed Jun 06, 2007 10:22 am 
Newbie

Joined: Wed Jan 17, 2007 11:50 am
Posts: 10
Hello,

I would like to check whether an SQL query has any results, but I don't need the returned entities/tuples. A simple solution would be:

SQLQuery query = session.createSQLQuery("...");
boolean hasResults = !query.list().isEmpty();

But this is very inefficient since list() creates all the entities (or in this case tuples) if I'm not mistaken.

Is there any way to check whether the query's ResultSet contains rows without actually creating any entities?

Thanks,
Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 10:58 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Don't think I've ever seen anything with that functionality built in. I know it's not the same query, but could you modify the query to just do select count(*) ... as the query?

Maybe projecting the results into nothingness? I don't know, just guessing :)

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 12:24 pm 
Beginner
Beginner

Joined: Sat May 12, 2007 2:55 am
Posts: 24
hi cbrehm,
Integer count = (Integer) session.createQuery("select count(*) from tablename").uniqueResult();

by using this you can count the number of query results without actually returning them.



nirav
(dont forget to rate if this helps)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 1:55 pm 
Regular
Regular

Joined: Mon May 08, 2006 6:00 am
Posts: 53
Location: India
have a generic method as shown below, make note of count(1) rather than count(*), as count(*) considers all the columns in each row, while calculation, count(1) gives more efficient results compared to count(*)



int queryStatus(String query){

String newquery = "select count(1) "+ query; // assumption, your query ll be "from tablename where crieteria=1"

Integer count = (Integer) session.createQuery(newquery).uniqueResult();

return count.intVal();

}

Also this depends on what you have in your where criteria of the query


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 5:57 am 
Newbie

Joined: Wed Jan 17, 2007 11:50 am
Posts: 10
Thanks for all your replies. But I am looking for a generic solution that assumes no knowledge of the query string and just tests whether the underlying ResultSet contains any rows, without causing Hibernate to transform all those rows into entities or tuples.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 6:28 am 
Beginner
Beginner

Joined: Sun Nov 19, 2006 6:18 am
Posts: 28
count(*) considers number of rows rite.Can u plz explain how the number of columns come into the picture.

I want to know how select count(1) is more efficient than select count(*) ....

Plz help

Thanks a lot in advance


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.