File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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
Author

Unable to map foreign key to a composite key in Hibernate

Ravi Pingali
Greenhorn

Joined: Nov 18, 2005
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,
Mohd. Yakub
Greenhorn

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

<property name="name"/>

<many-to-one name="watchRun" class="com.test.WatchRun"
insert="false" update="false">
<column name="watchRunId"/>
</many-to-one>
<many-to-one name="definition" class="com.test.DefinitionId"
insert="false" update="false">
<column name="definitionId"/>
</many-to-one>
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"/>
</many-to-one>

....

</class>
------------------------------
The collection of InstanceId in WatchRun would use:

<set name="instanceIds" inverse="true">
<key>
<column name="watchRunId"/>
</key>
<one-to-many class="com.test.InstanceId"/>
</set>
[ December 01, 2005: Message edited by: Mohd. Yakub ]
 
 
subject: Unable to map foreign key to a composite key in Hibernate
 
Similar Threads
Help with mapping set - Hibernate
Foreign key must have same number of columns as the referenced primary key
Hibernate2, composite-id, mappingException
Hibernate: Foreign key must have same number of columns as the referenced primary key
1-to-M mapping confusion