-->
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: composite ID with composite foreign key
PostPosted: Mon May 15, 2006 2:57 pm 
Newbie

Joined: Mon May 15, 2006 2:43 pm
Posts: 3
hi there,

I've got the following legacy database structure (3 tables):
USER, with seq_nr as pk (pk contains 1 column)
ORDER, with seq_nr, user as pk (pk contains 2 columns)
ORDER_ITEM, with seq_nr, order as pk (pk contains 3 columns)

I've mapped the ORDER table fine:
<class name="Order, DB" table="ORDR_ORDER">
<composite-id name="Pk" class="OrderPk, DB">
<key-property name="SequenceNumber" column="SEQ_NR" />
<key-many-to-one name="User"
class="User, DB"
column="FK_USER_SEQ_NR"/>
</composite-id>
</class>

I'm having trouble mapping the ORDER_ITEM table, however. The structure would look similar to the above. I'd specify a key-property (with name SequenceNumber), but I'd like to specify multiple columns in the <key-many-to-one> tag -- that the property is "Order" and the columns are FK_ORDR_SEQ_NR and FK_ORDR_USER_SEQ_NR.

Can I have a composite-id containing a foreign key to a table with more than 1 column in its primary key?

thanks,
Manoj


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 15, 2006 3:28 pm 
Newbie

Joined: Mon May 15, 2006 1:59 pm
Posts: 3
Your composite_id is missing <key-property name="user" column="USER"/> for ORDER table, and for ORDER_ITEM mapping instead of column="FK_USER_SEQ_NR" attribute use <key> element, something like:
<key>
<column name="FK_USER_SEQ_NR"/>
<column name="FK_ORDR_SEQ_NR "/>
</key>


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 16, 2006 7:12 am 
Newbie

Joined: Mon May 15, 2006 2:43 pm
Posts: 3
hey there,

I can use <key>, but that'll be to identify a relationship between order-item and order - not to define a composite identifier on order-item. NHibernate (Hibernate 2.1) doesn't let me have a <key> tag within a <composite-id> tag.

A possible solution (but impractical here - I'm working with an existing schema) would be to create a surrogate key (sequence number?) for order-item, and create a (composite) foreign key to order.

But - how do I specify an identifying composite-foreign-key relationship?

thanks,
Manoj


Top
 Profile  
 
 Post subject: solved
PostPosted: Sun May 21, 2006 8:18 am 
Newbie

Joined: Mon May 15, 2006 2:43 pm
Posts: 3
hi there,

I've got a suitable solution -- found this out by using MiddleGen to generate mapping files (great tool!)

I've got my composite-id tag defined as follows:
Code:
<composite-id name="Pk" class="OrderItemPk, DB">
   <key-property name="SequenceNumber" column="SEQ_NR" />
   <key-property name="OrderSequenceNumber" column="FK_ORDR_SEQ_NR" />
   <key-property name="UserSequenceNumber" column="FK_ORDR_USER_SEQ_NR" />
</composite-id>

Further down I define my composite foreign key (using the update="false" and insert="false" tags) referencing the same columns:
Code:
<many-to-one name="Order" class="Order, DB" update="false" insert="false">
   <column name="FK_ORDR_SEQ_NR" />
   <column name="FK_ORDR_USER_SEQ_NR" />
</many-to-one>


This allows me to define a complex primary-key, and still use parts of the primary key in relationships.

thanks,
Manoj


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.