gpt4 book ai didi

grails - 两个领域对象之间的多重关系

转载 作者:行者123 更新时间:2023-12-02 14:46:36 25 4
gpt4 key购买 nike

我有两个域类:ContractOrgainisation。契约(Contract)具有一个contractor(这是Orgaisation的一个实例)和许多/一个/没有beneficiaries(这也是Orgaisation的实例)。如何建立这些关系的模型?我希望Contract拥有这两种关系,以便可以执行以下操作:

contractInstance = new Contract()
contractInstance.addToBeneficiaries(name: 'A Company')
contractInstance.addToBeneficiaries(name: 'Other Company')
contractInstance.contractor = new Orgaisation('Antoher Company')
contractInstance.save()

我尝试了一些尝试,但仍收到错误消息( transient 值,对于多对多关系没有所属类,等等)

契约(Contract)
static belongsTo = [contractor:Organisation]
static hasMany = [beneficiaries:Organisation]

组织化
static hasMany = [contractorContracts:Contract, beneficiariesContracts:Contract]

我如何代表这些关系?

编辑:我忘了提到契约(Contract)受益人应该是一个多对多的协会(我想重用契约(Contract)中的受益人)。

最佳答案

我的解决方案是使用描述性名称明确显示M:M加入类。在您的情况下,似乎我们可以创建一个ProvidedService或类似的类,并具有:

Contract {
static belongsTo = [contractor: Organization]
static hasMany = [benefits: ProvidedService]
}
Organization {
static hasMany = [contractorContracts: Contract, receivedServices: ProvidedService]
}
ProvidedService {
//Feasibly there could be differences in the service provided to each beneficiary of a contract which could go in here.
static belongsTo = [contract: Contract, serviceRecipient: Organization]
}

关于grails - 两个领域对象之间的多重关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8468271/

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