I am not able to insert record in the child table with following relationship.
Parent table xml mapping
-------------------------------
<hibernate-mapping>
<class name="com.hibernate.PersonContactPo" table="PERSON_CONTACT" schema="ODSCUST_OWNER">
<composite-id name="id" class="com.hibernate.PersonContactPoId">
<key-property name="personId" type="big_decimal">
<column name="PERSON_ID" precision="22" scale="0" />
</key-property>
<key-property name="contactId" type="big_decimal">
<column name="CONTACT_ID" precision="22" scale="0" />
</key-property>
</composite-id>
<set name="contactRelationshipPos" inverse="true">
<key>
<column name="PERSON_ID" precision="22" scale="0" />
<column name="CONTACT_ID" precision="22" scale="0" not-null="true" />
</key>
<one-to-many class="com.hibernate.ContactRelationshipPo" />
</set>
</class>
</hibernate-mapping>
Child table xml mapping
----------------------------
<hibernate-mapping>
<class name="com.hibernate.ContactRelationshipPo" table="CONTACT_RELATIONSHIP" schema="ODSCUST_OWNER">
<composite-id name="id" class="com.hibernate.ContactRelationshipPoId">
<key-property name="contactId" type="big_decimal">
<column name="CONTACT_ID" precision="22" scale="0" />
</key-property>
<key-property name="contactMethodId" type="big_decimal">
<column name="CONTACT_METHOD_ID" precision="22" scale="0" />
</key-property>
</composite-id>
<many-to-one name="personContactPo" class="com.hibernate.PersonContactPo" update="false" insert="false" fetch="select">
<column name="PERSON_ID" precision="22" scale="0" />
<column name="CONTACT_ID" precision="22" scale="0" not-null="true" />
</many-to-one>
<many-to-one name="partyContactPo" class="com.rci.pom.hibernate.PartyContactPo" update="false" insert="false" fetch="select">
<column name="PARTY_ID" precision="22" scale="0" />
<column name="CONTACT_ID" precision="22" scale="0" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
I get the constraint error while hibernate try to insert record into the database. There is a constraint on child table which mandates either of the column PERSON_ID or PARTY_ID should exists but not both during insert.
Below is the Hibernate query from the log
------------------------------------------------
Hibernate: insert into ODSCUST_OWNER.PERSON_CONTACT (PERSON_ID, CONTACT_ID) values (?, ?)
Hibernate: insert into ODSCUST_OWNER.CONTACT_RELATIONSHIP (CONTACT_ID, CONTACT_METHOD_ID) values (?, ?)
From the log it shows only CONTACT_ID, CONTACT_METHOD_ID being inserted and PERSON_ID is not being inserted. I have set inverse="true" at parent table's xml mapping but still the same error. Potential cause of problem i feel is at child table which have attributes update="false" insert="false" which are set in the "many-to-one" tag. If i remove these attributes I get the following error -
Initial SessionFactory creation failed.org.hibernate.MappingException: Repeated
pPo column: CONTACT_ID (should be mapped with insert="false" update="false")
What is the workaround with this relationship ?
|