-->
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.  [ 4 posts ] 
Author Message
 Post subject: Please help: one-to-many, both with composite keys
PostPosted: Mon Mar 06, 2006 1:40 pm 
Beginner
Beginner

Joined: Mon Dec 26, 2005 3:29 pm
Posts: 23
Hi. We are having trouble mapping a one-to-many relationships with both ends' identity being composite keys (components). When trying to load it, we get
Hibernate version: 3.1

Mapping documents:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.acme" >
<class name="ItemDefinition" table="XRV_SER_DFN_ITMS">
<composite-id>
<key-property name="seriesName" column="SER_ID_R"></key-property>
<key-property name="effectiveDate" column="EFCT_D" ></key-property>
<key-property name="districtCode" column="DIST_C"></key-property>
<key-property name="itemCode" column="ITM_ID_C"></key-property>
</composite-id>
<property name="itemDescription" column="ITM_DESC_X" />
<property name="itemType" column="ITM_TYP_C" />
<property name="mdrm4Code" column="MDRM_C" />
<property name="itemSequence" formula="((MAX(MIC_CTL_SEQ_R) -1) * 220) + MAX(MIC_ITM_SEQ_R) - MAX(ITM_OCRN_R) + 1" />
<property name="occurrenceCount" formula="MAX(ITM_OCRN_R)" />
<many-to-one name="seriesDefinition">
<column name="ser_id_r" ></column>
<column name="efct_d"></column>
<column name="dist_c"></column>
</many-to-one>
</class>
</hibernate-mapping>


<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.acme">
<class name="SeriesDefinition" table="XRV_SER_DFN_CTL" >
<composite-id name="seriesDefinitionIdentity" class="com.acme.SeriesDefinitionIdentity">
<key-property name="seriesName" column="ser_id_r" />
<key-property name="effectiveDate" column="efct_d" />
<key-property name="districtCode" column="dist_c" />
<key-property name="seriesDefinitionStatusCode" column="ser_dfn_stat_c" />
</composite-id>
<set name="items" lazy="false">
<key unique="true" >
<column name="ser_id_r" ></column>
<column name="efct_d"></column>
<column name="dist_c"></column>
</key>
<one-to-many class="ItemDefinition" />
</set>



</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
SeriesDefinitionIdentity sdi = new SeriesDefinitionIdentity();
sdi.setSeriesName("FRY9LP");
sdi.setDistrictCode("02");
sdi.setEffectiveDate(new SimpleDate("20010331"));
sdi.setSeriesDefinitionStatusCode("P");

SeriesDefinition seriesDefinition = (SeriesDefinition)PersistenceEngineManager.getEngine("star").load(SeriesDefinition.class, sdi);

Full stack trace of any exception that occurs:
Foreign key (FK33F0F8465CA0D126:XRV_SER_DFN_ITMS [ser_id_r,efct_d,dist_c])) must have same number of columns as the referenced primary key (XRV_SER_DFN_CTL [ser_id_r,efct_d,dist_c,ser_dfn_stat_c])



Name and version of the database you are using: DB2


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 06, 2006 9:15 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You have speicfied that an ItemDefinition's primary key has 4 columns:
Code:
<key-property name="seriesName" column="SER_ID_R"></key-property>
<key-property name="effectiveDate" column="EFCT_D" ></key-property>
<key-property name="districtCode" column="DIST_C"></key-property>
<key-property name="itemCode" column="ITM_ID_C"></key-property>
But you're looking it up using only three columns from in the set:
Code:
<set name="items" lazy="false">
<key unique="true" >
<column name="ser_id_r" ></column>
<column name="efct_d"></column>
<column name="dist_c"></column>
</key>
<one-to-many class="ItemDefinition" />
</set>
You need to match up the columns better (using property-ref). Obviously the key in the set doesn't map exactly to the primary key (after all, it'd be a one-to-one if it did that). The easiest thing to do is to set up a <properties> tag in your ItemDefinition mapping, then refer to it using property-ref in the <key> element of the <set> element in SeriesDefinition.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 11:38 am 
Beginner
Beginner

Joined: Mon Dec 26, 2005 3:29 pm
Posts: 23
Thank for your reply.

I am still having some difficulties with this. I can't put the foreign properties into a <properties> tag because it is already present as part of ItemDefinition's composite keys (the <properties> example in the documentation - 5.1.14 - conveniently shows its use with a single identity property).

Also, if I try have the SeriesDefinition set <key> (one-to-many) element use a property-ref, then it complains that it can't find it within SeriesDefinition.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 5:27 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Something needs to be done to match up your columns, or else you've got a many-to-many mapping. Does seriesDefinitionStatusCode have to be part of the SeriesDefinition's primary key? If it wasn't there, your mapping would work. Given that it is there, should ItemDefinition also have that value? If you've got two SeriesDefinitions with the same seriesName, effectiveDate and districtCode, but different seriesDefinitionStatusCodes, which one owns the ItemDefinition with the same seriesName, effectiveDate and districtCode? You must get that relationship sorted before hibernate can figure out what's going on.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.