background image

Entity Inheritance Mapping Strategies

<< Mapped Superclasses | Single Table per Class Hierarchy Strategy >>
<< Mapped Superclasses | Single Table per Class Hierarchy Strategy >>

Entity Inheritance Mapping Strategies

Mapped superclasses are not queryable, and can't be used in EntityManager or Query
operations. You must use entity subclasses of the mapped superclass in EntityManager or
Query
operations. Mapped superclasses can't be targets of entity relationships. Mapped
superclasses can be abstract or concrete.
Mapped superclasses do not have any corresponding tables in the underlying datastore. Entities
that inherit from the mapped superclass define the table mappings. For instance, in the code
sample above the underlying tables would be FULLTIMEEMPLOYEE and PARTTIMEEMPLOYEE, but
there is no EMPLOYEE table.
Non-Entity Superclasses
Entities may have non-entity superclasses, and these superclasses can be either abstract or
concrete. The state of non-entity superclasses is non-persistent, and any state inherited from
the non-entity superclass by an entity class is non-persistent. Non-entity superclasses may not
be used in EntityManager or Query operations. Any mapping or relationship annotations in
non-entity superclasses are ignored.
Entity Inheritance Mapping Strategies
You can configure how the Java Persistence provider maps inherited entities to the underlying
datastore by decorating the root class of the hierarchy with the
javax.persistence.Inheritance
annotation. There are three mapping strategies that are
used to map the entity data to the underlying database:
A single table per class hierarchy
A table per concrete entity class
A "join" strategy, where fields or properties that are specific to a subclass are mapped to a
different table than the fields or properties that are common to the parent class
The strategy is configured by setting the strategy element of @Inheritance to one of the
options defined in the javax.persistence.InheritanceType enumerated type:
public enum InheritanceType {
SINGLE_TABLE,
JOINED,
TABLE_PER_CLASS
};
The default strategy is InheritanceType.SINGLE_TABLE, and is used if the @Inheritance
annotation is not specified on the root class of the entity hierarchy.
Entities
Chapter 24 · Introduction to the Java Persistence API
693