Inherited abstract class with JPA (+Hibernate)

From JPA 1.0 specification:

Both abstract and concrete classes can be entities. Both abstract and concrete classes can be annotated with the Entity annotation, mapped as entities, and queried for as entities.

Entities can extend non-entity classes and non-entity classes can extend entity classes.

As you want a single table, you should use Single Table inheritance.

Just define a discriminator column as follows:

@Entity
@DiscriminatorColumn(name="REF_TYPE")
public abstract class RefData {

But if you do not want to rely on JPA inheritance strategies, you can use MappedSuperclass instead:

@MappedSuperclass
public abstract class RefData {

JPA specification

An entity may inherit from a superclass that provides persistent entity state and mapping information, but which is not itself an entity. Typically, the purpose of such a mapped superclass is to define state and mapping information that is common to multiple entity classes.

Keep in mind you can not use @Entity and @MappedSuperclass at the same time.

Leave a Comment