【问题标题】:Create a Grails Domain mapping for a foreign key that is connected to the composite primary key of another Domain为连接到另一个域的复合主键的外键创建 Grails 域映射
【发布时间】:2015-07-14 02:38:53
【问题描述】:

如何将 Grails 域的两列(也是其复合主键)引用到另一个域类的复合主键?在这种情况下,CEOAgencyAgencyBranch 之间存在一对一的关系。

我可以轻松地将CEO 映射到Agency,因为它只涉及一列:CEO.id == Agency.ceo_id。但我无法使用这两列将Branch 映射到AgencyAgency.id == Branch.agency_id and Agency.main_branch_id == Branch.branch_id

class CEO {
    .....
}

class Agency {
    .....
    CEO ceo
    Branch mainBranch

    static mapping = {
        .....
        ceo column: 'ceo_id'
        /* Tried using this mapping but it doesn't work
        mainBranch {
            agencyId column: 'id',
            branchId column: 'main_branch_id'
        }
        */
        .....
    }
}

class Branch {
    .....
    Integer agencyId
    Integer branchId

    static mapping = {
        .....
        id composite: ['agencyId', 'branchId']
        .....
    }

    static constraints = {
        .....
        branchId unique: 'agencyId'
        .....
    }
}

【问题讨论】:

    标签: grails groovy


    【解决方案1】:

    请尝试对代理类使用以下技术:

     class Agency {
         .....
         CEO ceo
         Branch mainBranch
    
         static mapping = {
         .....
         ceo column: 'ceo_id'
         columns {
            mainBranch {
                column name: "FirstName"
                column name: "LastName"
            }
         }
     .....
     }
    

    参考是 here - 第 6.5.2.5 节复合主键。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 2013-12-09
      • 2017-08-06
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      相关资源
      最近更新 更多