-->
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.  [ 2 posts ] 
Author Message
 Post subject: Unable to map foreign key to a composite key in Hibernate
PostPosted: Fri Nov 18, 2005 6:30 am 
Newbie

Joined: Fri Nov 18, 2005 5:09 am
Posts: 1
Hi,

I am facing a peculiar situation where i have 2 fields(each as a foreign key) in a table and in the associated table these 2 fields together form a composite key(PK).
So i when i map each of these foreign keys to individual keys, i get the same error.
"
Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FKDF4D06517F636367:FileInstance [FileWatchRunId]))
must have same number of columns as the referenced primary key (FileWatchRun [FileWatchRunId,FileWatchDefinitionId])
"
Here are the mappings for ready reference.

====== Mappings for the composite key ======

<hibernate-mapping>
<class name="com.test.WatchRun" table="WatchRun">
<composite-id >
<key-property name="WatchRunId" column="WatchRunId" />
<key-many-to-one name="DefinitionId" class="com.test.Definition" column="DefinitionId" />
</composite-id>
</class>
</hibernate-mapping>

====== For the foreign key mapping ===============

<class name="com.test.Instance" table="Instance">
<id name="instanceId" column="InstanceId">
<generator class="increment"/>
</id>
<many-to-one name="WatchRunId" class="com.test.WatchRun" column="WatchRunId" not-null="true"/>
<many-to-one name="DefinitionId" class="com.test.Definition" column="DefinitionId" not-null="true"/>
</class>


A prompt response will be greatly appreciated.
Thanks,
============================


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 11:33 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
The docs explain how to do this here:

http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#components-compositeid

Here's a quote from the docs:

Quote:
Use the <composite-id> tag (with nested <key-property> elements) in place of the usual <id> declaration. For example, the OrderLine class has a primary key that depends upon the (composite) primary key of Order.

Code:
<class name="OrderLine">
   
    <composite-id name="id" class="OrderLineId">
        <key-property name="lineId"/>
        <key-property name="orderId"/>
        <key-property name="customerId"/>
    </composite-id>
   
    <property name="name"/>
   
    <many-to-one name="order" class="Order"
            insert="false" update="false">
        <column name="orderId"/>
        <column name="customerId"/>
    </many-to-one>
    ....
   
</class>

Quote:
Now, any foreign keys referencing the OrderLine table are also composite. You must declare this in your mappings for other classes. An association to OrderLine would be mapped like this:

Code:
<many-to-one name="orderLine" class="OrderLine">
<!-- the "class" attribute is optional, as usual -->
    <column name="lineId"/>
    <column name="orderId"/>
    <column name="customerId"/>
</many-to-one>

Quote:
(Note that the <column> tag is an alternative to the column attribute everywhere.)

_________________
nathan


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