我在复合类的 hibernate 映射方面遇到问题。 问题是我有 2 个类,它们仅限于 2 个表,而且我还有第三个类(篮子),它可以包含不同的类类型。
abstract class Fruit {...}
@Entity
@Table
class Apple extends Fruit {...}
@Entity
@Table
class Orange extends Fruit{...}
@Entity
@Table
class Basket {
...
@Column
private FruitType fruit_type;
// I dont know how to map composite class here
// It has to be mapped for two tables simultaneously
// which table has to be bound, depends on field fruit_type
private Fruit fruit;
... }
最佳答案
使用JPA inheritance 。
fruit_type
不需要在 Basket
中显式映射(如果不需要)。它应该与 Fruit
类相关(可以使用 @MappedSuperclass
进行注释),您可以将其指定为 @DiscriminatorColumn
。 Apple
和 Orange
中的每个都仅定义特定的 @DiscriminatorValue
。如果您愿意,它也可以是枚举
。
关于java - Hibernate 复合类映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36690385/