我有以下域类:
class AccountRecord {
List owners // owner of this account
}
和
class IndividualRecord {
String uniqueId
String secondaryId
List accounts // accounts owned by this individual
}
owners List
类中的AccountRecord
通过存储包含成对的每个所有者的uniqueId和secondaryId的映射来存储该帐户的所有所有者。因此,所有者列表可能如下所示:[
[uniqueId: 1, secondaryId 2] // owner 1
[uniqueId: 2, secondaryId 3] // owner 2
]
假设我要使用GORM查找所有者列表中包含 map 的所有
AccountRecord
,以便该 map 包含给定的IndividualRecord的uniqueId和secondaryId。基本上,给定一个个人,查找该个人拥有的所有帐户。目前,我正在执行此操作,但是它不起作用(由于某些原因,它还会退回个人不拥有的帐户):def accountsOwnedByUser = AccountRecord.where {[uniqueId: someOwner.uniqueId, secondaryId: someOwner.secondaryId] in owners}.list()
有什么建议么?
最佳答案
如果没有特殊原因不使用标准gorm M:N关系,则可以这样实现:
class AccountRecord {
static hasMany = [owners: IndividualRecord]
}
class IndividualRecord {
String uniqueId
String secundaryId
static belongsTo = AccountRecord
static hasMany = [accounts: AccountRecord]
}
Basically, given an individual, find all accounts that are owned by the individual
然后可以通过使用关联:
myIndividual.accounts
轻松实现。
关于grails - 具有动态参数的Gorm finder(第2部分)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24986405/