Pergunta

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.

Foi útil?

Solução

I didn't really get a chance to get back to writing an answer to this, but I found what the problem was. The issue was in these lines here:

<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>

I stupidly missed that the "column" specified in the elements of the bags is pointing to Policy_Id and Risk_Id, which is incorrect. It should be the name of the foreign key column that references the primary key of the entity where the one-to-many bags are defined. So in my case it should have been Party_Id and to differentiate between policy roles and party roles, I had to use a "where" constrant on the bag. So the definitions ended up looking like this:

<bag name="_policyRoles" access="field" table="Party_Role" where="Party_Role_Type = 'POLICY'">
  <key column="Party_Id" />
  <one-to-many class="com.blah.PartyRole" />
</bag>

<bag name="_riskRoles" access="field" table="Party_Role" where="Party_Role_Type = 'RISK'">
  <key column="Party_Id" />
  <one-to-many class="com.blah.PartyRole" />
</bag>

Outras dicas

Sounds to me that this is an error generated by your RDBMS. So, try to enable the SQL logging (hibernate.show_sql=true) and see what Hibernate is trying to do. But I would double-check the table structure for PARTY_ROLE, as it seems that the FKs were created with not-null constraints. There are some databases (Sybase, IIRC) that creates the FKs with not-null by default. And, if you specify the relationship as NULL, it may try to find a POLICY with the ID NULL, which certainly won't exist :-)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top