【问题标题】:Gorm finder with dynamic argument (part 2)?带有动态参数的 Gorm finder(第 2 部分)?
【发布时间】:2014-07-27 22:46:51
【问题描述】:

我有以下域类:

class AccountRecord {
     List owners // owner of this account
}

class IndividualRecord {
     String uniqueId
     String secondaryId
     List accounts // accounts owned by this individual
}

AccountRecord 类中的 owners List 通过存储包含每个所有者的 uniqueId 和 secondaryId 对的映射来存储该帐户的所有所有者。因此,所有者列表可能如下所示:

[
[uniqueId: 1, secondaryId 2] // owner 1 
[uniqueId: 2, secondaryId 3] // owner 2
]

假设我想使用 GORM 查找所有 AccountRecord 的所有者列表包含一个地图,该地图包含给定的 IndividualRecord 的 uniqueId 和 secondaryId。基本上,给定一个个人,找到该个人拥有的所有帐户。目前我正在这样做,但它不起作用(由于某种原因,它还返回不属于个人所有的帐户):

def accountsOwnedByUser = AccountRecord.where {[uniqueId: someOwner.uniqueId, secondaryId: someOwner.secondaryId] in owners}.list()

有什么建议吗?

【问题讨论】:

    标签: grails groovy


    【解决方案1】:

    如果没有特殊原因不使用标准 gorm M:N 关系,则可以这样实现:

    class AccountRecord {
      static hasMany = [owners: IndividualRecord]
    }
    
    class IndividualRecord {
    
      String uniqueId
      String secundaryId
    
      static belongsTo = AccountRecord
      static hasMany = [accounts: AccountRecord]
    }
    

    基本上,给定一个个人,找到该个人拥有的所有帐户

    这可以通过使用关联轻松实现:myIndividual.accounts

    【讨论】:

    • 谢谢。如果不使用关联,你能建议怎么做吗?欣赏它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 2013-05-18
    • 1970-01-01
    相关资源
    最近更新 更多