[英]Grails domain Named query for a list
I have a simple Grails application. 我有一个简单的Grails应用程序。 I have a couple of domains such as following.
我有几个领域,例如以下。 The scenario is Person has many Telephone (But person class does not have a list of telephone as a variable : Lazy Single-Ended Associations).
场景是“人有很多电话”(但人类没有将电话列表作为变量:懒惰单端关联)。
class Person implements Serializable {
....
}
class Telephone implements Serializable{
String number
static belongsTo = [person : Person]
static mapping = {
.....
person lazy: false
}
}
Now I have a requirement where I have to search the person by telephone numbers. 现在,我有一个必须通过电话号码搜索此人的要求。 I have a list of string telephone numbers.
我有一个字符串电话列表。 I need to get all the persons whom have at least one of that telephone number.
我需要让所有拥有至少一个该电话号码的人。 I need to write namedQueries, but I'm quite new to this area.
我需要编写namedQueries,但是我对这个领域还很陌生。 Is it possible to write named queries for this?
是否可以为此编写命名查询? Or do I need a mapping defined in Person class as
还是我需要在Person类中定义的映射为
set telephone
static hasMany = [
telephone: Telephone
]
And how would the namedQueries should be defined to suit my requirement 以及如何定义namedQueries以适合我的要求
Thanks in advance 提前致谢
I believe for it to be able to see telephone from person you are correct that a 我相信它能够看到您正确的人的电话
set telephone
static hasMany = [
telephone: Telephone
]
needs to be defined but once defined you can then just do: 需要定义,但是一旦定义,您可以执行以下操作:
static namedQueries = {
withSameTelephone {telephoneNumber ->
telephone{ eq 'number' telephoneNumber }
}
}
and use it like: 并像这样使用它:
Person.withSameTelephone('091511234512').list()
I think you need to pass a list so 我想你需要通过清单
static namedQueries = {
withSameTelephoneInList {telephoneNumberList ->
telephone{ 'in'( 'number' telephoneNumber) }
}
}
so you could do: 所以你可以这样做:
Person.withSameTelephoneInList(['091511234512','091511234513','091511234514']).list()
I'd stick with single-ended o2m, and put a named query like: 我会坚持使用单端o2m,然后输入一个命名查询,例如:
class Telephone {
...
static namedQueries = {
byPerson{ Person p ->
eq 'person', p
}
}
}
and call it like: 并这样称呼:
Person p = Person.get 1111
def telephones = Telephone.byPerson p
On the other hand you can use a simple findAllBy*
query: 另一方面,您可以使用简单的
findAllBy*
查询:
Person p = Person.get 1111
def telephones = Telephone.findAllByPerson p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.