2011-12-11 5 views
1

도메인 클래스가 두 개 있습니다 (ContractOrgainisation). 계약서에는 contractor (Orgaisation의 인스턴스)과 many/one/none beneficiaries (인스턴스는 Orgaisation 임)이 있습니다. 이러한 관계를 어떻게 모델링합니까? 나는 몇 가지를 시도두 도메인 개체 간의 다중 관계

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

하지만 (오류 메시지를 받고 일시적 값, 다 대다 관계 등 없음 소유 클래스를 유지 : 내가 좋아하는 뭔가를 할 수 있도록 둘 다 관계를 소유 Contract을 원한다 ...)

계약

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] 
} 
관련 문제