-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Single column foreign key to composite primary key
PostPosted: Thu Jan 26, 2006 2:20 pm 
Newbie

Joined: Thu Jan 26, 2006 2:09 pm
Posts: 1
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 8:31 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
you can't do it with mapping - foreign key must have same number of (and type) columns in database - it is request from relational design
you can try resolve this with interceptors


Top
 Profile  
 
 Post subject: Re: Single column foreign key to composite primary key
PostPosted: Thu Jan 26, 2006 11:11 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
You might look at the section in the docs "Custom SQL for Loading" It's 16.5 in the 3.1 docs.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.