Hibernate HQL 和 Grails - 如何比较集合?
我有一个 HQL 问题(在 Groovy/Grails 中),希望有人能帮助我。
我有一个简单的 Asset 对象,带有一对多 Tags 集合。
类资产{ 设置标签 静态 hasMany = [标签:标签] }
类标签{ 字符串名称 用户
我在 HQL 中尝试做的事情:
在 params.tags 中传入一些标签(例如:groovy grailsrocks),并希望返回具有这些标签的资产标签,并且只有那些确切的标签。
这是我的 HQL,如果资产标签中存在一个或多个标签,则返回资产:
SELECT DISTINCT a 从 资产一 左连接 a.标签t 在哪里 t.name IN (:tags)
assetList = Asset.executeQuery( hql, [tags:tokenizedTagListFromParams]
上面的代码工作完美,但它实际上就像一个 OR
. 如果找到任何标签,它将返回该资产,
我只想返回具有完全相同的标签(数量也相同)的资产
。 new Tag(name:xxx).save() 这样我就可以获取所要求的每个标签的标签实例和唯一 ID,
我还尝试使用 Tag.findByName(t1)
每个标签,以及上面 HQL 中的(唯一)标签 ID 列表,如果没有运气,
我将不胜感激。
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.name 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决这个问题需要花费太多的脑力。您是否考虑过使用可搜索插件?您可以将 Tag 设置为 Asset 的“组件”,然后进行类似“+tag:groovy +tag:grails +tag:rocks”的搜索。这会让你得到你想要的结果(而且可能更快)。
It's taking too much brain effort to work this one out. Have you considered using the Searchable plugin instead? You could set up Tag as a "component" of Asset and then do a search like "+tag:groovy +tag:grails +tag:rocks". That would get you the results you're after (and probably quicker).