考虑以下实体。我打算让 Child 类引用两个派生食品类( Local Food 或 Foreign )中的任何一个。这是一个人为的示例,我的真实域对象非常复杂,因此组合和使用例如 FoodType 列不是一个选项,因为两个 Food 子类仅在少数特征上相似。
@MappedSuperclass
public abstract class Food {
}
@Entity
public class LocalFood extends Food {
private long id;
private String name;
}
@Entity
public class ForeignFood extends Food {
private long id;
private String name;
}
@Entity
public class Child {
private Food food; //Base Class needed here
@ManyToOne()
public Food getFood() {
return food;
}
}
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.sample.Child.food references an unknown entity: com.sample.Food
也不使用继承和鉴别器。
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Food {
private long id; // set , get (Auto gen)
}
是否有可能使这种映射起作用?