Grails GORM:按集合内容查找

Grails GORM:按集合内容查找,grails,hql,gorm,Grails,Hql,Gorm,我有以下两门课: class Person{ String name static hasMany = [ items: Item ] } class Item{ String name } 多个人也可以拥有相同的项目。我正试图得到一份所有人的名单,他们的收藏中有一个特定的项目。即,人员A和B的列表中都有项目A,因此将其全部返回。不幸的是,他们的不是findAllByCollectionContains(),最近的是findAllByCollection(),它需要一

我有以下两门课:

class Person{

 String name

  static hasMany = [
     items: Item
 ]
}

class Item{
 String name
}
多个人也可以拥有相同的项目。我正试图得到一份所有人的名单,他们的收藏中有一个特定的项目。即,人员A和B的列表中都有项目A,因此将其全部返回。不幸的是,他们的不是findAllByCollectionContains(),最近的是findAllByCollection(),它需要一个精确的集合

我一直在尝试executeQuery给我更多的控制权,但还没有想出任何办法

我尝试过的示例:

 Person.executeQuery("select name from Person p where ? in p.items",[item])

有什么建议吗?

您必须加入items集合,然后您可以轻松地查询它

 Person.executeQuery("select name from Person p join p.items as i where i = ?",[item])

这很有效。但是你能解释一下为什么它会起作用吗?谢谢当您加入
p.items
时,现在可以基于单个项目而不是项目集合进行查询。看看HQL文档: