Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3491366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:37:41+00:00 2026-05-18T11:37:41+00:00

I am having trouble mapping the following database structure (shortened for brevity with just

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:37:42+00:00Added an answer on May 18, 2026 at 11:37 am

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble deleting orphan nodes using JPA with the following mapping @OneToMany
I'm having trouble mapping the following in NHibernate: I have two tables (these are
having a little trouble with a mapping for the following table setup currently: Shop
I am having trouble mapping the following JDK JCE encryption code to Bouncy Castles
I'm having some trouble figuring out the appropriate FluentNHibernate mapping syntax for the following
I am having trouble deleting orphan nodes using Hibernate with the following mapping @OneToMany(fetch
I'm using Hibernate's JPA impl to model some tables. I'm having trouble mapping a
I am having a trouble mapping a specific URL request to one of the
I am having trouble with inheritance mapping in Linq to Sql. I am using
Having trouble with the following segment of code. I'm getting a parameter count mismatch.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.