This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Unable to map foreign key to a composite key in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Unable to map foreign key to a composite key in Hibernate" Watch "Unable to map foreign key to a composite key in Hibernate" New topic

Unable to map foreign key to a composite key in Hibernate

Ravi Pingali

Joined: Nov 18, 2005
Posts: 1

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 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 ======

<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" />

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

<class name="com.test.Instance" table="Instance">
<id name="instanceId" column="InstanceId">
<generator class="increment"/>
<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"/>

A prompt response will be greatly appreciated.
Mohd. Yakub

Joined: Nov 25, 2005
Posts: 18
What i get is that the Instance class has a primary key that depends upon the (composite) primary key of WatchRun & Definition.
try this...

<class name="com.test.Instance">

<composite-id name="id" class="com.test.InstanceId">
<key-property name="instanceId"/>
<key-property name="watchRunId"/>
<key-property name="definitionId"/>

<property name="name"/>

<many-to-one name="watchRun" class="com.test.WatchRun"
insert="false" update="false">
<column name="watchRunId"/>
<many-to-one name="definition" class="com.test.DefinitionId"
insert="false" update="false">
<column name="definitionId"/>
Now, any foreign keys referencing the InstanceId table are also composite. You must declare this in your mappings for other classes. An association to InstanceId would be mapped like this:

<many-to-one name="InstanceId" class="com.test.InstanceId">
<!-- the "class" attribute is optional, as usual -->
<key-property name="instanceId"/>
<key-property name="watchRunId"/>
<key-property name="definitionId"/>


The collection of InstanceId in WatchRun would use:

<set name="instanceIds" inverse="true">
<column name="watchRunId"/>
<one-to-many class="com.test.InstanceId"/>
[ December 01, 2005: Message edited by: Mohd. Yakub ]
I agree. Here's the link:
subject: Unable to map foreign key to a composite key in Hibernate
It's not a secret anymore!