-->
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.  [ 8 posts ] 
Author Message
 Post subject: Some difficulties with this HQL query
PostPosted: Mon Jan 26, 2009 2:35 pm 
Newbie

Joined: Sat Nov 29, 2008 6:30 pm
Posts: 8
Hi,

I guess I would have the same problem with SQL, I'm a beginner for both. Anyway, here is a HQL query that seems to give me the right result:

Code:
@"select Parent
    from Parent parent
        join parent.Children ch
        where (ch.Type = :chType) and
              (select count(*) from parent.Children) = 1")


What I want with that is get all parents that have only one entry in the Children table and this single entry has a particular value for a field.

What I'm not sure is if this is the right way to achieve what I want. Is it performant? In my version, is the count(*) triggered for each child of a parent (so if there is N children, the same count would be triggered N time)?

Thanks for your help.

Nicolas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 6:13 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
To see if your query is good or bad in terms of execution time and plan you would have to take the time it takes to execute and if the execution plan for the generated SQL is bad in any way.

I personally would write it like this:
Code:
from Parent parent join parent.children ch where ch.type = :chType group by parent having count(parent.children)=1


You don't need the part before "from" if you only want the main object (parent in this case)
If i'm not mistaken the property names (ch.Type, parent.Children) should be in lowercase but if it works that way too it's ok i guess.

Hope that helps, rating appreciated


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 6:48 pm 
Newbie

Joined: Sat Nov 29, 2008 6:30 pm
Posts: 8
With this query I get an exception:

Code:
unindexed collection before [] [from Parent parent
                       join parent.Children ch
                       where (ch.Type = :chType)
                       group by parent
                       having count(parent.Children) = 1]


I should add that "Children" is a Set if that matters.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 7:39 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
try just count(ch) or count(ch.id) (or whatever the id is called) instead of count(parent.Children)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 9:14 pm 
Newbie

Joined: Sat Nov 29, 2008 6:30 pm
Posts: 8
No luck, I have now:

Column 'Parents.State' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

State is one of the columns of my Parents table.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 1:22 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
phew... Although i'm pretty sure there's an easier way you could try add all the parents properties to the group by clause


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 3:24 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Yes, pkleindl is right, you have to include all selected (non-aggregated) properties (event though on some databases your query works). This has to be done until HHH-1615 is fixed.

BTW: In HQL you can also use size() to get a collections size:
Code:
from Parent p where size(p.children) = 1
or in your case:
Code:
from Parent parent join parent.children ch where ch.type = :chType group by [parent's properties] having size(parent.children)=1

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 11:28 am 
Newbie

Joined: Sat Nov 29, 2008 6:30 pm
Posts: 8
With these changes I still get the "unindexed collection before []" error.
Anyway, I will stick to the first version for the moment, using size() which will shorten the query a bit. Thanks to all.


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