grails - 具有动态参数的Gorm finder(第2部分)?

标签 grails groovy

我有以下域类:

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/

相关文章:

grails事务 Controller ,处理异常

jenkins - 是否可以在管道脚本中将字符串与作业参数连接起来

gradle - 在gradle中创建任务的语法

groovy - 应用程序代码中的gradle build属性

grails - 从Grails 3中的 Controller 获取所有方法

list - 将多个复选框绑定(bind)到 Grails 中的列表

jquery - 可以访问Jquery中的GSP元素

rest - spring-security-rest插件注销失败

groovysh 找不到方法

unit-testing - Grails设置了系统属性进行测试