0

I have 2 tables: Currency and Rate

The currency's primary key is referenced by the inputCurrency and outputCurrency foreigh keys in the Rate.

How can I map these in order to cascadeALL?

I tried in Rate (the table that contains the foreigh keys)

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "INPUT_CURRENCY")
private Currency inputCurrency;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "OUTPUT_CURRENCY")
private Currency ouputCurrency;

But if I delete an item from currency It results a primary key violation.I can't remove a Currency becouse it is referenced by inputCurrency or outputCurrency in Rate.

If I want to annotate the list of rates in Currency, how can I say mappedBy = "inputCurrency" and "outputCurrency" ?

@OneToMany(cascade = CascadeType.ALL,mappedBy ="inputCurrency,outputCurrency")
List<Rate> rates;
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nelly Junior
  • 556
  • 2
  • 10
  • 30
  • clearly you cannot use mappedBy with multiple values because it makes no sense. "mappedBy" = "where is this bidirectional relation stored" ... and it can't be stored in multiple places! – Neil Stockton May 03 '16 at 10:06

2 Answers2

1

You can settle with two collections:

List<Rate> inputRate;

List<Rate> outputRate;

Each with a corresponding mappedBy

and a third @Transient List<Rate> rates;

You can use a @PostLoad event to unite the two persistent lists into the transient one.

And similarly, when adding/removing elements to one of the persistent lists, make sure to add/remove from the joint list.

Ori Dar
  • 18,687
  • 5
  • 58
  • 72
1

The solution is using two list of rates in Currency: one for the fk inputCurrency and another for the fk outputCurrency

@OneToMany(cascade = CascadeType.ALL,mappedBy ="inputCurrency")
List<Rate> ratesIC;

@OneToMany(cascade = CascadeType.ALL,mappedBy ="outputCurrency")
List<Rate> ratesOC;
Nelly Junior
  • 556
  • 2
  • 10
  • 30