我想出了这种映射方法:
@Entity
@Inheritance(InheritanceType.TABLE_PER_CLASS)
public abstract class Tax {
@Id
@Column(name = "taxRateId")
//@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk-sequence")
//@SequenceGenerator(name = "pk-sequence", sequenceName = "ID_GEN", allocationSize = 1)
protected Long taxRateId = -1;
public long getTaxRateId() {
return taxRateId;
}
@Column(nullable=false)
private double taxRate;
@Column(nullable=false)
private String countryName; // countryType ?
// getters and setters for 'taxRate' and `countryName`
}
接下来,我们定义两个具体的Tax实体,如下所示:
@Entity
public class AustrianTax extends Tax {
// nothing special here, the type differentiates enough
}
@Entity
public class GermanTax extends Tax {
// nothing special here, see above --^
}
然后,我们以通用方式将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<Tax> countryRates;
// getters and setters, add and remove for 'countryRates'
}
然而,将其定义如下会更精确 - 并且符合 3NF 中的数据模型:
/*
* 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<Tax> countryRates;
// getters and setters, add and remove for 'countryRates'
}
后一种情况的一般假设/基本原理 (@ManyToMany):
某本书(同一本书)在不同国家/地区销售,税率不同。因此,如果可以的话,我的建议是更改数据库架构(表结构)。这将/将产生一个 n:m 表来映射关系
|Book| 0..* <--> 0..* |Tax|
正确(在语义上)。脚注:我希望你还处于开发的早期阶段。
唯一剩下的是属性countryType,我无法将其关联为Book 类型的属性,假设一本书没有针对奥地利方言进行修改,因此打印为不同的内容(在这里说德语;-))。然而,对于大多数 D-A-CH 书籍来说,这并不常见。
希望这会有所帮助。