【发布时间】:2012-05-10 07:24:28
【问题描述】:
我无法确定一个 GORM 查询以匹配对象上的多个关联:
class Zoo {
String name
static hasMany = [animals:Animal]
static namedQueries = {
// SEARCH1 match any of a list of animals
searchOr { searchAnimals ->
or {
searchAnimals.each { name ->
animals {
eq('name', name)
}
}
}
}
// SEARCH2 match ALL of a list of animals
searchAnd { searchAnimals ->
and {
searchAnimals.each { name ->
animals {
eq('name', name)
}
}
}
}
}
}
class Animal {
String name
}
SEARCH1 很乐意将动物园与动物列表中的任何一个进行匹配,但是应该如何编写 SEARCH2 以获取在提供的列表中包含所有动物的动物园?
【问题讨论】:
-
在 searchAnimals.each 下移动 and 会发生什么{}
-
@jonaldomo 我认为当前的
and无论如何都是隐含的,因此没有必要。将其移至searchAnimals.each {},隐含的and仍然存在,而新的and将仅应用于单个语句。尝试不会有什么坏处 - 会让你知道会发生什么。
标签: hibernate grails associations grails-orm h2