I am trying to create a OneToMany relationship between two tables where the destination table has a compound id. One column of the compound id is the one I want to key to, yet I can't figure it out. Example tables:
TABLE EXCHANGE_RATE (
CURRENCY_ID bigint(20),
YEAR bigint(20),
RATE float(13,4));
TABLE PRICE (
PRICE_ID bigint(20),
CURRENCY_ID bigint(20),
VALUE float(13,4));
The EXCHANGE_RATE table has multiple rows with a given CURRENCY_ID, but the combination of CURRENCY_ID and YEAR is always unique. So I've defined a compound id on EXCHANGE_RATE that includes CURRENCY_ID and YEAR.
However the fk in the PRICE table, CURRENCY_ID, only maps to the CURRENCY_ID column of the EXCHANGE_RATE table. I've tried everything I can think of to map the PRICE object to an EXCHANGE_RATE object and can't it right. Here is an example of my annotations in the Price.java class:
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "CURRENCY_ID",
referencedColumnName = "CURRENCY_ID",
insertable = false, updatable = false)
public Set<ExchangeRate> getExchangeRates() {
return this.exchangeRates;
}
I've also tried the following annotations:
leaving out referencedColumnName:
@JoinColumn(name = "CURRENCY_ID", insertable = false,
updatable = false)
using referencedColumnName with ID_ prepended:
@JoinColumn(name = "CURRENCY_ID", referencedColumnName =
"ID_CURRENCY_ID", insertable = false, updatable = false)
using referencedColumnName with ID. prepended:
@JoinColumn(name = "CURRENCY_ID", referencedColumnName =
"ID.CURRENCY_ID", insertable = false, updatable = false)
Does anybody know how to make this work? Thanks in advance.