-->
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.  [ 3 posts ] 
Author Message
 Post subject: hql query 2
PostPosted: Sat Feb 02, 2008 6:36 am 
Beginner
Beginner

Joined: Thu Aug 09, 2007 3:48 pm
Posts: 38
suppose i need a sub query to return the age of the 20th child, to use somewhere:

select ...
from ...
where age > AGE_OF_20TH_CHILD

how can i form that as subquery?
select age
from T
where ????

thanks


Top
 Profile  
 
 Post subject: Don't use a subquery
PostPosted: Sat Feb 02, 2008 10:45 am 
Newbie

Joined: Tue Jan 22, 2008 9:53 am
Posts: 18
You don't need to use a subquery.

if you need the 20th child you should make a query like this:

"FROM table_name ORDER BY AGE DESC"

So set the query.setFirstResult(19); // 19 because it start counting by 0
and set the query.setMaxResults(1); // Return just 1 result;
This is a simple query and will retrieve only one result: the 20th child ordered by AGE in descendind order.

This is the correct and efficient way to do this in hibernate.

Hope to have solved your problem


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 02, 2008 10:59 am 
Beginner
Beginner

Joined: Thu Aug 09, 2007 3:48 pm
Posts: 38
This does not solve my problem.
What you suggest is making a separate query only to retrieve the 20th age.
I dont want to make 2 queries.

I have 1 query, where one of the many conditions is age > 20th_age
thus i needed the sub query.
I cant do setMaxResults on my final query, because the ordering there is different and everything there is different.


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