我有两个域类:Contract
和Orgainisation
。合同有一个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()
我尝试了一些事情,但不断收到错误消息(瞬态值,没有多对多关系的拥有类等等)
static belongsTo = [contractor:Organisation]
static hasMany = [beneficiaries:Organisation]
static hasMany = [contractorContracts:Contract, beneficiariesContracts:Contract]
我如何表示这些关系?
编辑:我忘了提及合同受益人应该是一个多对多的协会(我希望在合同中重复使用受益人)。答案 0 :(得分:1)
我的解决方案是使用描述性名称明确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]
}