Hi
I am facing a problem in saving data to a parent child relationship tables in MySQL. The scenario is
Parent Table with Auto Increment Key (Database Generated AUTO_INCREMENT of MySQL).
Child Table with Composite Key (One part of the same is the parent tables key and other is a user defined value).
The Mapping for Parent Table contains the ID declaration and the association to child as
<id name="parentPk" type="int">
<column name="PARENT_PK" />
<generator class="native" />
</id>
<set name="child" inverse="true" cascade="all">
<key>
<column name="parent_id" not-null="true" />
</key>
<one-to-many class="childClass" />
</set>
For the Child Table Two Mappings have been tried
Mapping 1)
<composite-id name="id" class="CompositeId.class">
<key-property name="child.user.define.key" type="string">
<column name="childPk1" length="50" />
</key-property>
<key-property name="parentPk" type="int">
<column name="parent_id" not-null="true" />
</key-property>
</composite-id>
<many-to-one name="parent" class="parent.class" insert="false" update="false">
<column name="parent_id" not-null="true" />
</many-to-one>
With the above mapping, a database integrity constrain violation error is received. Hibernate does not set the foreign key into the child table by retrieving the primary key from the parent table. (Tried the identity generator as well).
Mapping 2)
<composite-id name="id" class="CompositeId.class">
<key-property name="child.user.define.key" type="string">
<column name="childPk1" length="50" />
</key-property>
<key-many-to-one name="parentPk" class="parent.class">
<column name="parent_id" not-null="true" />
</key-property>
</composite-id>
<many-to-one name="parent" class="parent.class" insert="false" update="false">
<column name="parent_id" not-null="true" />
</many-to-one>
With this mapping Hibernate throws an IllegalArgumentException from BasicGetter class. On further investigation, it is evident that Hibernate is trying to invoke get method on java.lang.Integer rather than on the parent class.
Any help on this problem would be appreciated.
Regards
Tarun
|