Multiple @MappedSuperclass

别等时光非礼了梦想. 提交于 2019-12-18 14:54:16

问题


I'm using JPA 2.0 and EclipseLink 2.2.0.

I have a @MappedSuperclass, AbstractEntity, that is the basis for all my entities providing PK and auditing columns.

I want to have another @MappedSuperclass extend that class and be the root for a TABLE_PER_CLASS inheritance strategy.

At present, when building with Maven I receive header errors.

Are multiple @MappedSuperclass allowed in an inheritance hierarchy?


回答1:


Multiple mapped superclasses are allowed in same inheritance hierarchy. It is not directly said so in specifications, but JPA 2.0 specification does not explicitly prohibit multiple mapped superclasses, and in other context it refers to case with multiple mapped superclasses in same hierarchy:

The default access type of an entity hierarchy is determined by the placement of mapping annotations on the attributes of the entity classes and mapped superclasses of the entity hierarchy that do not explicitly specify an access type.

This means that you can do following:

@MappedSuperclass
public class FirstMapped {
    String firstVal;
}

@MappedSuperclass
public class SecondMapped extends FirstMapped {
    String secondVal;
}

@Entity
public class ExtendingEntity extends SecondMapped {
    @Id int id;
}

Mapped superclass cannot be root of entity inheritance. Root of the entity inheritance must be entity, as told in documentation. With EclipseLink adding @Inheritance to the one of the mapped superclasses in example above is silently ignored. Adding @Inheritance to to the ExtendingEntity works as expected - it becomes root of entity inheritance hierarchy.

In general mapped superclasses are only for allowing reuse of mappings and they are not part of entity inheritance.

If this does not answer to your question, it would help if you can share those "header errors".




回答2:


Are multiple @MappedSuperclass allowed in an inheritance hierarchy?

Yes, I have done this. To be able to answer your question about maven errors, you'll have to provide a stacktrace and code..



来源:https://stackoverflow.com/questions/6226392/multiple-mappedsuperclass

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!