gpt4 book ai didi

grails - 为连接到另一个域的复合主键的外键创建 Grails 域映射

转载 作者:行者123 更新时间:2023-12-02 14:41:39 26 4
gpt4 key购买 nike

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

我可以轻松映射CEOAgency因为它只涉及一列:CEO.id == Agency.ceo_id .但我无法映射BranchAgency使用这两列:Agency.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'
.....
}
}

最佳答案

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

 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 节复合主键。

关于grails - 为连接到另一个域的复合主键的外键创建 Grails 域映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31396730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com