Grails:如何查询域类? hasMany-relationship 反对相交的对象集?
我有以下内容:
class Object_1 {
static hasMany = [tags:Tag]
Set tags;
...
}
现在我有一组标签,并且想要查找具有相交(!=匹配)标签的所有 Object_1 实例。我正在考虑类似的事情
Object_1.findAllByTagsInList(tags);
,但这根本不起作用 - 我得到一个“嵌套异常是 org.hibernate.exception.SQLGrammarException:无法执行查询”。我有一种感觉,我错过了一些重要的事情。非常感谢帮助。
I have about the following:
class Object_1 {
static hasMany = [tags:Tag]
Set tags;
...
}
Now I have a set of tags and want to find all Object_1-instances with intersecting (!= matching) tags. I was thinking of something like
Object_1.findAllByTagsInList(tags);
But that does not work at all - I get a "nested exception is org.hibernate.exception.SQLGrammarException: could not execute query". I have the feeling I am missing something important. Help highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我实际上找到了解决问题的优雅方法。我将关系重新设计为多对多,这样可以简单地迭代标签列表来查找所有相关对象。
...当然现在我必须多次处理这种关系 - 但我很高兴能在很少的地方进行这种工作。
I actually found an elegant way to solve the problem. I redesigned the relationship to be many-to-many which allows for simply iterating over the tags list finding all the relevant objects.
... of course now I have to take care of that relationship a couple of times - but I am happy to have this working with few locs.
“in list”不是动态查找方法中识别的运算符之一 - 这是行不通的。
相反,您必须使用 HQL 或条件生成器 来制定您的查询(也许将其放入静态查找方法中)。
"in list" is not one of the operators recognized in dynamic finder methods - that won't work.
Instead, you'll have to use HQL or the criteria builder to formulate your query (and perhaps put it in a static finder method).