Hibernate HQL and Grails- How do I compare collections?
Posted
by BurtP
on Stack Overflow
See other posts from Stack Overflow
or by BurtP
Published on 2010-05-03T04:26:30Z
Indexed on
2010/05/03
4:38 UTC
Read the original article
Hit count: 306
Hi everyone (my first post!),
I have an HQL question (in Groovy/Grails) I was hoping someone could help me with.
I have a simple Asset object with a one-to-many Tags collection.
class Asset {
Set tags
static hasMany = [tags:Tag]
}
class Tag {
String name
}
What I'm trying to do in HQL:
A user passes in some tags in params.tags
(ex: groovy grails rocks) and wants to return Asset(s) that have those tags, and only those exact tags.
Here's my HQL that returns Assets if one or more of the tags are present in an Assets tags:
SELECT DISTINCT a
FROM
Asset a
LEFT JOIN
a.tags t
WHERE
t IN (:tags)
assetList = Asset.executeQuery( hql, [tags:tokenizedTagListFromParams]
The above code works perfect, but its really like an OR
. If any of the tag(s) are found, it will return that Asset.
I only want to return Assets that have those exact same tags (in number as well).
Every time a new tag is created, I new Tag(name:xxx).save()
so I can get the Tag instances and unique ID's for each tag that was asked for.
I also tried converting the passed in tags to a list of Tag instances with Tag.findByName(t1)
for each tag, and also a list of (unique) Tag Id's into the HQL above with no luck.
I would appreciate any help/advice.
Thank you for your time, Burt
© Stack Overflow or respective owner