Legacy database structure Hibernate mapping issue -
i having trouble mapping following database structure (shortened brevity pks/fks , few 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 partyrole table can contain row links party policy and/or row links same party risk. many many join table combines both many many relationships: party<->policy , 1 party<->risk. party_role_type can either policy or party , acts discriminator identify relationship row belongs to.
i've tried model structure 4 entities: policy, party, risk, partyrole. here 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 java pojos setup match mapping , associations setup correctly when objects added or deleted in collections. policy considered aggregate root, when saved hibernate want save parties associated policy. when add party policy , risk (and associated roles) following exception:
caused by: java.sql.batchupdateexception: integrity constraint violation: foreign key no parent; fk_party_role_policy table: party_role
what wrong? best way map relationship? there chance map relationship somehow without use of intermediate entity? help.
i didn't chance writing answer this, found problem was. issue 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 "column" specified in elements of bags pointing policy_id , risk_id, incorrect. should name of foreign key column references primary key of entity one-to-many bags defined. in case should have been party_id , differentiate between policy roles , party roles, had use "where" constrant on bag. definitions ended looking 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>
Comments
Post a Comment