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…)
contract
static belongsTo = [contractor:Organisation]
static hasMany = [beneficiaries:Organisation]
orgainisation
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).
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
ProvidedServiceor something like that and have: