Reputation: 11307
I have two domain classes: Contract
and Orgainisation
. A contract has one contractor
(which is an instance of Orgaisation
) and many/one/none beneficiaries
(which are instances of Orgaisation
also). How do I model these relationships? I want Contract
to own both relationships so that I can do something like:
contractInstance = new Contract()
contractInstance.addToBeneficiaries(name: 'A Company')
contractInstance.addToBeneficiaries(name: 'Other Company')
contractInstance.contractor = new Orgaisation('Antoher Company')
contractInstance.save()
I tried a few things but keep getting error messages (transient value, no owning class for a many-to-many relationship and so on...)
static belongsTo = [contractor:Organisation]
static hasMany = [beneficiaries:Organisation]
static hasMany = [contractorContracts:Contract, beneficiariesContracts:Contract]
How do I represent these relationships?
EDIT: I forgot to mention the contract-beneficiary should be a many-to-many association (I want to reuse beneficiaries accross contracts).
Upvotes: 1
Views: 201
Reputation: 4742
My solution is to make explicit the M:M joining class with a descriptive name. In your case it seems like we can make a class ProvidedService
or something like that and have:
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]
}
Upvotes: 1