Multiple @MappedSuperclass

前端 未结 2 1546
天命终不由人
天命终不由人 2021-01-01 17:03

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.

<
相关标签:
2条回答
  • 2021-01-01 17:29

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

    0 讨论(0)
  • 2021-01-01 17:39

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

    0 讨论(0)
提交回复
热议问题