鉴于以下域名结构:
Book {
static hasMany = [tags: Tag]
}
Tag {
String name
}
我正试图找到一种给出Book
的方法,我可以找到包含本书任何标签的任何其他书籍。
我试过了:
Book.findAllByTagsInList(myBook.tags)
但正如预期的那样,“List in List”查询未产生所需的结果。
答案 0 :(得分:2)
您可以将标准用作
def books = Book.createCriteria().listDistinct{
tags{
'in'('id', myBook.tags*.id)
}
}
或使用HQL作为
def books = Book.executeQuery("select distinct b from Book as b \
inner join b.tags as tag \
where tag.id in (:tags)",
[tags: myBook.tags*.id])