2011-12-11 69 views
1

我有兩個域類:ContractOrgainisation。合同有一個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] 

orgainisation

static hasMany = [contractorContracts:Contract, beneficiariesContracts:Contract] 

我如何表達這些關係?

編輯:我忘了提及合同 - 受益人應該是一個多對多的協會(我想重新使用合同的受益人)。

回答

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] 
} 
相關問題