Hi all,
I am having trouble mapping the following database structure (shortened for brevity with just PKs/FKs and a few extra columns:
Policy
---------------
Policy_Id (PK)
...
Risk
---------------
Risk_Id (PK)
...
Party
---------------
Party_Id (PK)
...
PartyRole
---------------
PartyRole_Id (PK)
Party_Id (FK not-null)
Policy_Id (FK)
Risk_Id (FK)
Party_Role_Type
So the PartyRole table can contain a row that links a party to a Policy and/or a row that links the same party to a Risk.
Basically it is a many to many join table but it combines both many to many relationships: Party<->Policy and one for Party<->Risk. Party_Role_Type can be either POLICY or PARTY and acts effectively as a discriminator to identify which relationship the row belongs to.
I've tried to model this structure with a 4 entities: Policy, Party, Risk, PartyRole. Here are the mappings:
Code:
<class name="com.blah.Party" table="Party">
<id column="Party_Id" name="_id" type="int" unsaved-value="-1" access="field">
<generator class="sequence">
<param name="sequence">SQ_Party</param>
</generator>
</id>
<bag name="_policyRoles" access="field" table="Party_Role">
<key column="Policy_Id" />
<one-to-many class="com.blah.PartyRole" />
</bag>
<bag name="_riskRoles" access="field" table="Party_Role">
<key column="Risk_Id" />
<one-to-many class="com.blah.PartyRole" />
</bag>
</class>
<class name="com.blah.Risk" table="Risk">
<id column="Risk_Id" name="_id" type="int" unsaved-value="-1" access="field">
<generator class="sequence">
<param name="sequence">SQ_Risk</param>
</generator>
</id>
<bag name="_partyRoles" access="field">
<key column="Risk_Id" />
<one-to-many class="com.blah.PartyRole" />
</bag>
</class>
<class name="com.blah.Policy" table="Policy">
<id column="Policy_Id" name="_id" type="int" unsaved-value="-1" access="field">
<generator class="sequence">
<param name="sequence">SQ_Policy</param>
</generator>
</id>
<bag name="_partyRoles" inverse="true" cascade="save-update" access="field" table="Party_Role" >
<key column="Policy_Id" />
<one-to-many class="au.com.cgu.harvest.domain.party.PartyRole" />
</bag>
</class>
<class name="au.com.cgu.harvest.domain.party.PartyRole" table="Party_Role" schema="Harvest">
<id column="Party_Role_Id" name="_id" type="int" unsaved-value="-1" access="field">
<generator class="sequence">
<param name="sequence">Harvest.SQ_Party_Role</param>
</generator>
</id>
<property name="partyRoleType" column="PARTY_ROLE_TYPE"
type="java.lang.String" />
<many-to-one name="_party" column="Party_Id" class="com.blah.Party" access="field" cascade="save-update" fetch="join" />
<many-to-one name="_risk" column="Risk_Id" class="com.blah.Risk" access="field" />
<many-to-one name="_policy" column="Policy_Id" class="com.blah.Policy" access="field" />
</class>
All the java pojos are setup to match this mapping and all associations are setup correctly when objects added or deleted in collections. Policy is considered an aggregate root, so when it is saved by Hibernate I want to save the Parties associated with the Policy. When I add a Party to the Policy and Risk (and all the associated roles) I get the following exception:
Caused by: java.sql.BatchUpdateException: integrity constraint violation: foreign key no parent; FK_PARTY_ROLE_POLICY table: PARTY_ROLE
What is wrong? Also is this the best way to map this relationship? Is there a chance to map this relationship somehow _without_ the use of the intermediate entity? Thanks for all you help.
When