Grails域名列表名称查询
问题描述:
我有一个简单的Grails应用程序。我有几个域如下。情景是人有很多电话(但人类没有一个电话列表作为变量:懒惰单端关联)。Grails域名列表名称查询
class Person implements Serializable {
....
}
class Telephone implements Serializable{
String number
static belongsTo = [person : Person]
static mapping = {
.....
person lazy: false
}
}
现在我有一个要求,我必须通过电话号码搜索人。我有一串字符串电话号码。我需要得到所有至少有一个电话号码的人。我需要编写namedQueries,但我对这个领域很陌生。是否有可能为此编写命名查询?或者我需要将在namedQueries应定义为满足我的要求提前
感谢Person类定义为
set telephone
static hasMany = [
telephone: Telephone
]
以及如何映射
答
我相信它是能够看到的人的电话,你是正确的,一个
set telephone
static hasMany = [
telephone: Telephone
]
需求是但一旦定义您可以然后只是做:
static namedQueries = {
withSameTelephone {telephoneNumber ->
telephone{ eq 'number' telephoneNumber }
}
}
并使用它像:
Person.withSameTelephone('091511234512').list()
我认为你需要传递一个列表,以便
static namedQueries = {
withSameTelephoneInList {telephoneNumberList ->
telephone{ 'in'('number' telephoneNumber) }
}
}
,所以你可以这样做:
Person.withSameTelephoneInList(['091511234512','091511234513','091511234514']).list()
答
我会与单端O2M坚持,并把像一个命名查询:
class Telephone {
...
static namedQueries = {
byPerson{ Person p ->
eq 'person', p
}
}
}
,并调用它像:
Person p = Person.get 1111
def telephones = Telephone.byPerson p
在另一方面,你可以使用一个简单findAllBy*
查询:
Person p = Person.get 1111
def telephones = Telephone.findAllByPerson p