-->
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: Is string foreign key in junction table a problem ?
PostPosted: Mon Jun 08, 2009 7:08 am 
Newbie

Joined: Mon Jun 08, 2009 6:51 am
Posts: 1
Hello

I'm trying to load an entity from a junction table. The mapping I'm using is :
Code:

Item <- *ItemCountry(x,y,z)* -> Country

class ItemCountry {
...
   @ManyToOne
   @JoinColumn(name = "country_code")
   public Country getCountry() {
      return country;
   }
...
}



ItemCountry contains a numeric foreign key from Item, and a foreign key from Country which is the country code (string).


The outcome of these HQLs is quite confusing (never had this with numeric FKs):

from ItemCountry -> EntityNotFoundException: Unable to find com.comp.core.model.Country with id US

select ic from ItemCountry ic where ic.country.code='US' -> EntityNotFoundException: Unable to find com.comp.core.model.Country with id US

but :

from Country where country.code ='US' -> Country: {code=US, code3=USA, ...}

select ic.country.code from ItemCountry ic where ic.country.code ='US' -> US


So it looks like ItemCountry cannot construct a reference to Country. Is that because of the type of the foreign key from Country ?

Here is also the DDL of the junction table.

Code:

CREATE TABLE `item_country` (
  `item_country_id` int(11) NOT NULL auto_increment,
  `item_id` int(11) default NULL,
  `country_code` varchar(2) default NULL,
  PRIMARY KEY  (`item_country_id`),
  KEY `item_id` (`item_id`),
  KEY `country_code` (`country_code`),
  CONSTRAINT `item_country_fk` FOREIGN KEY (`item_id`) REFERENCES `item` (`item_id`),
  CONSTRAINT `item_country_fk1` FOREIGN KEY (`country_code`) REFERENCES `country` (`country_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;




Any idea ?

Ta.


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.