我有以下类设置,使用SINGLE_TABLE继承:
@Entity
@Table(name = "ANIMALS")
@DiscriminatorColumn(name="DISCR")
@DiscriminatorValue("Animal")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Animal {
private String discr;
...
}
@Entity
@Table(name = "ANIMALS")
@DiscriminatorValue("Cat")
public class Cat extends Animal {
private String discr;
...
}
@Entity
@Table(name = "PERSONS")
public class Person {
@ManyToOne
@JoinColumn(name = "ANIMAL_ID", referencedColumnName = "ID")
private Animal animal;
@ManyToOne
@JoinColumn(name = "ANIMAL_ID", referencedColumnName = "ID")
private Cat cat;
...
}
在Person
课程中,我与@ManyToOne
和Animal
课程有两个Cat
关系。我希望Hibernate根据鉴别器映射到正确的字段(animal
或cat
)。使用此代码时,Hibernate在尝试将Animal
记录映射到Cat
对象时会抛出错误。