JPA supports (implementation) inheritance using
the @javax.persistence.Inheritance
annotation,
and this is fully supported by JPA Objects. The
annotation is used to determine how to map clases, using the strategy
attribute to specify the InheritanceType
enum:
InheritanceType.SINGLE_TABLE
A single table per class hierarchy
InheritanceType.TABLE_PER_CLASS
A table per concrete entity class
InheritanceType.JOINED
A strategy in which fields that are specific to a subclass are mapped to a separate * table than the fields that are common to the parent class, and a join is performed * to instantiate the subclass.
The @Inheritance
annotation goes on the
superclass.
For the SINGLE_TABLE
and
JOINED
inheritance mapping strategies a further
column is added to the table which is used to specify the concrete
subclass that the data held in the table corresponds. For the
JOINED
mapping it also effectively specifies the
subclass table to join to. Typically the column name and type is
specified explicitly on the superclass using the
@javax.persistance.DiscriminatorColumn
annotation; the @DiscriminatorValue
annotation is
then used on the subclasses to specify the values that go into this
column.
As we've already noted though in Section 2.5, “Specify a discriminator”, JPA Objects
always requires an @DiscriminatorValue
to be
specified. Rather than invent adhoc discriminators for each inheritance
hierarchy, JPA Objects in effect forces us to
standardize the values across the entire domain model.
What this also means is that, if a
@DiscriminatorColumn
has been specified, then the
discriminatorType
(if specified) must be
DiscriminatorType.STRING
. The length should be long
enough for the longest discriminator; a length of 3 is generally
sufficient.