JPA 继承 TABLE_PER_CLASS 抛出 MappableContainerException

JPA Inheritance TABLE_PER_CLASS throws MappableContainerException

JPA 继承有问题,服务器抛出错误。已经有很多关于该主题的问题,但我找不到解决我的问题的方法,所以这里是我到目前为止的详细信息...

超类:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn(name = "PERSON_TYPE", discriminatorType = DiscriminatorType.INTEGER)
@Table(name = "PERSON")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TRIGGER_PERSON_PRIMARY_KEY")
    @SequenceGenerator(name = "TRIGGER_PERSON_PRIMARY_KEY", sequenceName = "PERSON_AI_SEQUENCE", allocationSize = 1, initialValue = 1)
    @Column(name = "PERSON_ID", updatable = false, unique = true, nullable = false)
    protected int personId;

子类:

@Entity
@Table(name = "APPLICANT")
@DiscriminatorValue(value = "1")
@PrimaryKeyJoinColumn(name="APPLICANT_ID")
public class Applicant extends Person {
}

错误信息:

The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container

Caused by: java.lang.ClassCastException: org.hibernate.mapping.UnionSubclass cannot be cast to org.hibernate.mapping.RootClass

你不能在 super class 和 sub class 中都有 @Id .

解决方案

从子 class 中删除 @Id Applicant

更多:

  1. Spring 3.1 Hibernate 4 exception for Inheritance [cannot be cast to org.hibernate.mapping.RootClass]
  2. Java/Hibernate JPA: InheritanceType.TABLE_PER_CLASS and IDs

我终于设法解决了这个问题。我已经采取了几个步骤:

  1. 另一个 class 扩展 Person 抛出错误,所以我修复了依赖关系。
  2. 我已经将继承类型更改为 JOINED,这实际上是我正在寻找的 - 不同的 tables 用于不同的列,一个根 table 用于常见的东西。
  3. 作为 2. 的结果,也不再需要保留 @PrimaryKeyJoinColumn。