多个@MappedSuperclass [英] Multiple @MappedSuperclass

查看:88
本文介绍了多个@MappedSuperclass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JPA 2.0和EclipseLink 2.2.0.

I'm using JPA 2.0 and EclipseLink 2.2.0.

我有一个@MappedSuperclass抽象实体,这是我所有提供PK和审计列的实体的基础.

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

我想让另一个@MappedSuperclass扩展该类并成为TABLE_PER_CLASS继承策略的根.

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

目前,在使用Maven进行构建时,我收到标头错误.

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

继承层次结构中是否允许多个@MappedSuperclass?

Are multiple @MappedSuperclass allowed in an inheritance hierarchy?

推荐答案

在同一继承层次结构中允许多个映射的超类.在规范中并没有直接说出来,但是JPA 2.0规范没有明确禁止多个映射超类,而在其他情况下,它是指在同一层次结构中具有多个映射超类的情况:

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;
}

映射的超类不能是实体继承的根.如文档所述,实体继承的根必须是实体.使用EclipseLink在上述示例中将@Inheritance添加到映射的超类之一中时,将被忽略.将@Inheritance添加到ExtendingEntity可以按预期方式工作-它成为实体继承层次结构的根.

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".

这篇关于多个@MappedSuperclass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆