Hi,
I am not able to insert in 2 parent table, 1 child table scenario.
I have 2 parent tables(say P1, P2), 1 child table(say C).
Primary keys: P1-->p1; P2-->p2; C-->p1+p2(These keys are foreign keys and primary keys).
In hbm files, the mapping given as below:
Parent P1 has Set mapping to child C.
Parent P2 has Set mapping to child C.
Child C has <key-many-to-one mapping to both Parents P1, P2.
I took domain Object for P1 populated with P2 and C.
When I call session.save(p1_domainObject); hibernate generates insert query for P1, C. Since P2 does not have record, getting exception: ConstraintViolationException: violated - parent key not found
I want to insert into both the Parents and child by saying session.save(p1_domainObject);. Please suggest me mapping.
Thanks in advance.
Mapping for Child table
Code:
<hibernate-mapping>
<class name= "Child_Class"
table="Child_Table">
<composite-id>
<key-many-to-one name="parent1" class="Parent1" column="p1" />
<key-many-to-one name="parent2" class="Parent2" column="p2" />
</composite-id>
</class>
</hibernate-mapping>
Mapping for Parent Table-1
Code:
<hibernate-mapping>
<class name= "Parent1"
table="Parent_Table_1" dynamic-insert="false">
<id name="p1" column="p1" type="java.lang.String">
<generator class="assigned"/>
</id>
<set name="userKeyValue" table="Child_Table" lazy="true" inverse="true" cascade="all">
<key column="p1"/>
<one-to-many class="Child_Class"/>
</set>
</class>
</hibernate-mapping>
Mapping for Parent Table-2
Code:
<hibernate-mapping>
<class name= "Parent2"
table="Parent_Table_2" dynamic-insert="false">
<id name="p2" column="p2" type="java.lang.String">
<generator class="assigned"/>
</id>
<set name="userKeyValue" table="Child_Table" lazy="true" inverse="true" cascade="all">
<key column="p2"/>
<one-to-many class="Child_Class"/>
</set>
</class>
</hibernate-mapping>