Hibernate单表继承和多个@ManyToOne

时间:2016-04-19 10:14:56

标签: java hibernate jpa inheritance

我有以下类设置,使用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课程中,我与@ManyToOneAnimal课程有两个Cat关系。我希望Hibernate根据鉴别器映射到正确的字段(animalcat)。使用此代码时,Hibernate在尝试将Animal记录映射到Cat对象时会抛出错误。

0 个答案:

没有答案