如何在不止一个JPA值的情况下使用mappingBy [英] How to use mappedBy with more than one value JPA
问题描述
我有2张桌子: 货币和汇率
I have 2 tables: Currency and Rate
币种的主键由Rate中的eighboring的inputCurrency和outputCurrency引用.
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?
我尝试使用Rate(包含foreeigh键的表)
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;
但是,如果我从货币中删除项目,则会导致主键冲突.我无法删除货币,因为它由Rate中的inputCurrency或outputCurrency引用.
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.
如果我想用Currency注释汇率列表,我怎么能说mapledBy ="inputCurrency"和"outputCurrency"?
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;
推荐答案
该解决方案使用两种汇率列表:一种用于fk inputCurrency 另一个用于fk outputCurrency
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;
这篇关于如何在不止一个JPA值的情况下使用mappingBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!