-->
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.  [ 1 post ] 
Author Message
 Post subject: Composite Foreign Key that maps to Unique Constraint
PostPosted: Mon Sep 13, 2010 4:31 pm 
Newbie

Joined: Mon Sep 13, 2010 4:21 pm
Posts: 1
Hi I have 2 tables (table1 & table2). Table 1 has 2 columns(table1ColA & table1ColB) which are unique. Table2 has 2 similar columns which I define as foreign keys of Table1.

So Table2 (table2ColA & table2ColB) == foreign key reference to Table1(table1ColA & table1ColB)

When I try to generate the Hibernate Mapping Files, this is the error I get

"org.hibernate.MappingException: Foreign key (FKCB773E2441174225:table2 [table2ColA,table2ColB])) must have same number of columns as the referenced primary key (table1 [id])"

Why is hibernate trying to pick table1's primary key as the referenced primary key when in mySQL the foreign key is defined as (table1ColA & table1ColB) which have unique constraint on them? How do I tell Hibernate to use the foreign key defined in mySQL as the foreign key and not assume that the primary key of the referenced table is the foreign key? Table definition can be found below. Thanks!

Code:
CREATE TABLE IF NOT EXISTS `table1` (
  `id` int(10) NOT NULL DEFAULT '0',
  `table1ColA` varchar(10) DEFAULT NULL,
  `table1ColB` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `table1ColA_table1ColB` (`table1ColA`,`table1ColB`)
)

CREATE TABLE IF NOT EXISTS `table2` (
  `id` int(10) NOT NULL DEFAULT '0',
  `table2ColA` varchar(10) DEFAULT NULL,
  `table2ColB` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK_table2_table1` (`table2ColA`,`table2ColB`),
  CONSTRAINT `FK_table2_table1` FOREIGN KEY (`table2ColA`, `table2ColB`) REFERENCES `table1` (`table1ColA`, `table1ColB`)
)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.