[英]How to use mappedBy with more than one value JPA
I have 2 tables: Currency and Rate 我有2张桌子:货币和汇率
The currency's primary key is referenced by the inputCurrency and outputCurrency foreigh keys in the Rate. 币种的主键由“费率”中的foreigh键的inputCurrency和outputCurrency引用。
How can I map these in order to cascadeALL? 我如何映射这些以便进行层叠?
I tried in Rate (the table that contains the foreigh keys) 我在Rate(包含foreigh键的表)中尝试过
@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. 但是,如果我从货币中删除项目,则会导致主键冲突。我无法删除货币,因为它由Rate中的inputCurrency或outputCurrency引用。
If I want to annotate the list of rates in Currency, how can I say mappedBy = "inputCurrency" and "outputCurrency" ? 如果要注释货币中的汇率列表,我怎么能说mapledBy =“ inputCurrency”和“ outputCurrency”?
@OneToMany(cascade = CascadeType.ALL,mappedBy ="inputCurrency,outputCurrency")
List<Rate> rates;
The solution is using two list of rates in Currency: one for the fk inputCurrency and another for the fk outputCurrency 解决方案是使用两种汇率列表:一种用于fk inputCurrency,另一种用于fk outputCurrency
@OneToMany(cascade = CascadeType.ALL,mappedBy ="inputCurrency")
List<Rate> ratesIC;
@OneToMany(cascade = CascadeType.ALL,mappedBy ="outputCurrency")
List<Rate> ratesOC;
You can settle with two collections: 您可以选择以下两个集合:
List<Rate> inputRate;
List<Rate> outputRate;
Each with a corresponding mappedBy
每个都有一个对应的
mappedBy
and a third @Transient List<Rate> rates;
第三个
@Transient List<Rate> rates;
You can use a @PostLoad
event to unite the two persistent lists into the transient one. 您可以使用
@PostLoad
事件将两个持久列表组合成一个临时列表。
And similarly, when adding/removing elements to one of the persistent lists, make sure to add/remove from the joint list. 同样,将元素添加到持久列表之一或从其中删除时,请确保从联合列表中添加/删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.