Jpa 为什么';太平绅士';s EntityManager#getReference返回@MappedSuperclass的子类?
给定以下人为的JPA实体继承关系:Jpa 为什么';太平绅士';s EntityManager#getReference返回@MappedSuperclass的子类?,jpa,eclipselink,Jpa,Eclipselink,给定以下人为的JPA实体继承关系: @MappedSuperclass @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn(name = "SUB_TYPE") abstract class SuperEntity @Entity @DiscriminatorValue("SUB") class SubEntity extends SuperEntity 为什么下面的方法调用不返回类子实体的实例 Entit
@MappedSuperclass
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "SUB_TYPE")
abstract class SuperEntity
@Entity
@DiscriminatorValue("SUB")
class SubEntity extends SuperEntity
为什么下面的方法调用不返回类子实体的实例
EntityManager em;
Long idOfSub = 42;
SuperEntity subAsSup = em.getReference(SuperEntity.class, idOfSub);
这种意外行为背后的原因是什么?什么替代代码可以按预期工作
澄清:我实际上不想做出(可能是硬编码的)决定,我是否希望返回类子实体的实例或其他类型的子实体。在我的真实场景中,客户端代码通过类SuperEntity
的API进行通信,而不关心子实体的具体类型。SuperEntity不是实体。这是一个映射超类。因此,继承和鉴别器列注释是无用的。如果希望它是一个实体,则需要使用实体对其进行注释。相关报道:我读了你提到的另一个问题。你是说我也应该用@Entity
注释@MappedSuperclass
,以实现我所追求的实体多态性?是的,如果你想要实体多态性,那么超实体必须是一个实体。