-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can you order hql results based on matching restrictions?
PostPosted: Thu Feb 05, 2009 6:14 pm 
Newbie

Joined: Thu May 01, 2008 11:52 am
Posts: 17
I have an object Dog which holds a set of DogMetadata.

Each DogMetadata has two values: a String ("desc") and an int ("rank").

Descriptions of dogs have different rankings such as: "small" is 5, "furry" is 2, "friendly" is 9, "dalmation" is 11, "mutt" is 22.

I need to search for dogs based on any of their desc values (for example, find "furry" OR "small" dogs).

This query returns the matching dogs, but they are not in any order.

Code:
select distinct Dog as d
left join d.dogMetadata as dMeta
where ( (dMeta.desc = 'furry') OR (dMeta.desc = 'small') )


How do I order the list of matching Dog objects by the total "rank" values of any matching DogMetadatas?

I've been working like a dog on this all day (trying "Group By" and "Order By") but I think I have been barking up the wrong tree.


Top
 Profile  
 
 Post subject: Re: Can you order hql results based on matching restrictions
PostPosted: Thu Feb 05, 2009 11:43 pm 
Newbie

Joined: Sun Oct 05, 2008 2:45 pm
Posts: 8
Hi,
You told that you tried Order By, please also try using fetch and check if it works.
select distinct Dog d
left join fetch d.dogMetadata dMeta
where ( (dMeta.desc = "furry") OR (dMeta.desc = "small") )
order by dMeta.desc asc


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 12:51 pm 
Newbie

Joined: Thu May 01, 2008 11:52 am
Posts: 17
Thank you, that *almost* works. There are two problems.

1. I need to Dogs who match multiple desc to rank higher in the list (example: a dog who matches both 'fluffy' and 'friendly' would rank higher than a dog who only matches 'fluffy'. Do I need to use sum or count? I am not sure how to do that with HQL.

2. I can only get the Dog objects to sort correctly when I use desc. If I use asc, there doesn't seem to be any ordering. I am using Oracle 11 and Hibernate 3.2.1.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 1:51 pm 
Newbie

Joined: Thu May 01, 2008 11:52 am
Posts: 17
So I am working with this query:

Code:
select d from Dog as d
left join fetch d.dogMetadata as dMeta
where ( (dMeta.desc = 'furry') OR (dMeta.desc = 'small') )
group by d
order by sum(dMeta.rank) desc


But can't get it to work... "ORA-00979 not a GROUP BY expression".

This led me to this bug report:
http://opensource.atlassian.com/project ... tion_24530

So.. I am confused.

(1) is my new query the right way to solve my dog ordering problem?

(2) if so, how do I work around that bug?


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