After using hibernate successfully for many situations, I have finally encountered a problem I've yet to solve myself. The situation comes about in trying to map legacy data in a 25 year old table structure which unfortunately uses composite keys. I'd like to change the table structure, but I don't have enough AS400 knowledge to be confident that I'm not breaking the legacy programs. Therefore, I'm stuck with the current structure.
This particular situation is for an insurance company. The parent table utilizes a composite-key comprising the columns alpha1, alpha2, pol#. I have no problem mapping this table alone.
The problem I'm having is trying to map a set of items(itemmast table) to the policy(polmast table). The itemmast table ties into the polmast table through the column pol#. In other words, all items with pol#=A1123 match the policy with alpha1=A, alpha2=1, and pol#=123.
Here is what I've tried:
Code:
<class name="com.baldwinmutual.Polmast" table="BMICVT.POLMAST">
<composite-id name="id" class="com.baldwinmutual.PolicyNumber">
<key-property name="alpha1" type="string" length="1">
<column name="ALPHA1" />
</key-property>
<key-property name="alpha2" type="string" length="1">
<column name="ALPHA2" />
</key-property>
<key-property name="pol" type="string">
<column name="`POL#`" />
</key-property>
</composite-id>
....
<set name="items" table="BMICVT.ITEMMAST">
<key>
<column name="`POL#`" />
</key>
<one-to-many class="com.baldwinmutual.Itemmast" />
</set>
</class>
and
<class name="Itemmast" table="BMICVT.ITEMMAST">
<composite-id name="id" class="ItemNumber">
<key-property name="policyNumber" type="string" length="7">
<column name="`POL#`" />
</key-property>
<key-property name="sequence" type="big_decimal" >
<column name="`SEQ#`" />
</key-property>
</composite-id>
.....
<many-to-one name="polmast" class="Polmast" insert="false" update="false" not-null="true">
<column name="ALPHA1" />
<column name="ALPHA2" />
<column name="`POL#`" />
</many-to-one>
</class>
When testing the mapping, Hibernate fails to initialize with the following error:
Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FK673BF9F3B3754B76:BMICVT.ITEMMAST [POL#])) must have same number of columns as the referenced primary key (BMICVT.POLMAST [ALPHA1,ALPHA2,POL#])
Exception in thread "main" java.lang.ExceptionInInitializerError
I understand why the error is being thrown but is there a solution? It is the same problem found in post
http://forum.hibernate.org/viewtopic.php?t=947370 but the solution there was coding. Is this still the only solution?
Thanks,
Kevin