各位开发人员:,
我目前的情况:我使用的是jpa2.1,在一个表中,我有一列在外键关系中引用了两个不同的表。
如何在jpa中描述这一点?
Table Book
taxRateId (manyToOne) (GermanTax/AustrianTax)
countryType
Table GermanTax
taxRateId (oneToMany Books)
Table AustrianTax
taxRateId (oneToMany Books)
CONSTRAINT germanTax FOREIGN KEY (tax_rate_id) REFERENCES german_tax (id)
CONSTRAINT austrianTax FOREIGN KEY (tax_rate_id) REFERENCES austrian_tax (id),
1条答案
按热度按时间lf3rwulv1#
我想到了这种Map方法:
接下来,我们定义两个具体的税务实体,如下所示:
然后,我们绘制Map
Book
以一种通用的方式Tax
```/*
As you brought up the question
*/
@Entity
public class Book {
// fields & annotations for ID attribute and generation strategy, similar to above
@OneToMany
private Set countryRates;
// getters and setters, add and remove for 'countryRates'
}
/*
One book can refer to 1 or more rates (Austrian/German) AND
One rate can be applied to 1 or more books -> @ManyToMany
*/
@Entity
public class Book {
// fields & annotations for ID attribute and generation strategy, similar to above
@ManyToMany
private Set countryRates;
// getters and setters, add and remove for 'countryRates'
}