Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple @MappedSuperclass

Tags:

jpa-2.0

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?

like image 941
retrodev Avatar asked Jun 03 '11 11:06

retrodev


2 Answers

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

like image 186
Mikko Maunu Avatar answered Feb 07 '23 05:02

Mikko Maunu


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

like image 26
beerbajay Avatar answered Feb 07 '23 04:02

beerbajay