This is regarding saving parent and childs where child has composite key and that composite key is made of parent primary key(native generated) and other reference table primary key(native generated)
in detail there are three tables
1) Parent 2) Child and 3) Reference
parent contains set of childs and has its own primary key with column name in table parent_id
child has a composite key this composite key is made of foerien keys of parent_id and reference_id
reference table has its own primary key with column name reference_id
Mapping file for Parent
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="com.test" >
<class name="Parent" table="PARENT" schema="TESTING">
<id column="PARENT_ID" name="id">
<generator class="native" />
</id>
<version name="updatedDateTime" column="UPDATED_DTM" type="timestamp" />
<set name="child" inverse="true" cascade="all-delete-orphan" >
<key column="PARENT_ID" not-null="true" />
<one-to-many class="com.test.Child" />
</set>
...
...
...
</class>
</hibernate-mapping>
Mapping file for child
Code:
<hibernate-mapping>
<class name="com.test.Child" table="CHILD" schema="TESTING">
<meta attribute="extends" inherit="false">com.test.AbstractModelObject</meta>
<composite-id name="childId" class="com.test.ChildId">
<key-property name="parentId" type="java.lang.Long">
<column name="PARENT_ID" />
</key-property>
<key-property name="chartFieldId" type="java.lang.Short">
<column name="REFERENCE_ID" />
</key-property>
</composite-id>
<property column="CHILD_IND" name="indicator" type="string" />
<version name="updatedDateTime" column="UPDATED_DTM" type="timestamp" />
<many-to-one name="reference" class="com.test.Reference" update="false" insert="false">
<column name="REFERENCE_ID" not-null="true" />
</many-to-one>
<many-to-one name="parent" class="com.test.Parent" update="false" insert="false">
<column name="PARENT_ID" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
Mapping file for Reference Table
Code:
<hibernate-mapping
package="com.test">
<class name="Reference" schema="TESTING" table="REFERENCE">
<id column="REFERENCE_ID" name="id">
<generator class="native" />
</id>
<version name="updatedDateTime" column="UPDATED_DTM" type="timestamp" />
<property column="REFERENCE_NM" name="name" type="string" />
<property column="REFERENCE_DS" name="description" type="string" />
</class>
</hibernate-mapping>
I was able to read the existing records using the mapping files and POJO, but has problem only while saving Object
when tried to save the parent Object it internally trying to save all the childs using bidirectional mapping but it is not creating and updating the values in
ChildId even though the reference objects of
parent property and
reference property objects are populated which intern throws following exception
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.test.Child