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.