6.13.2 Inheritance mapping strategy

When an entity is inherited, you can specify the method of mapping the class hierarchy to a table as the inheritance mapping strategy. You specify the inheritance mapping strategy by using @Inheritance or the inheritance tag of the entity tag in the O/R mapping file.

There are three types of inheritance mapping strategies:

However, with Cosminexus JPA Provider, the TABLE PER CLASS strategy is not available. If the TABLE PER CLASS strategy is used when Cosminexus JPA Provider is used, an exception occurs when the application starts.

Note
When you use Cosminexus JPA Provider, note the following points:
  • You cannot combine and specify multiple inheritance strategies in a class hierarchy. Also, no check is performed to verify whether multiple inheritance strategies are combined. If multiple inheritance strategies are specified, the operations might not function properly.
  • If the column specified in @DiscriminatorColumn is defined in an entity field, the value set in the entity field is not applied to the database even if the persist operation and then commit is executed. The value specified in @DiscriminatorValue or the default value is applied. Also, note that after commit, the value set in the field before commit is stored as is.

These strategies are specified in the value of the enumeration type javax.persistence.DiscriminatorType. The following points describe each strategy:

Organization of this subsection
(1) SINGLE TABLE strategy
(2) JOINED strategy

(1) SINGLE TABLE strategy

The SINGLE TABLE strategy is a strategy method of mapping all the classes present in the inheritance hierarchy of the entity class to one table. Therefore, the table must have an identification column as the column for identifying the class.

Specify the identification column in @DiscriminatorColumn or in the O/R mapping file. The default identification column name is DTYPE. If an identification column does not exist in the database, an exception occurs during the execution of the application.

If you want to specify the value stored in the identification column, use @DiscriminatorValue or the discriminator-value tag beneath the entity tag of the O/R mapping file.

Note
When you use the SINGLE TABLE strategy, the user must be able to specify a null value in the table column corresponding to the subclass field.

(2) JOINED strategy

The JOINED strategy is a strategy method of mapping the top-level of the class hierarchy to a single table. Each subclass is indicated by the subclass-specific fields that are not inherited from the superclass and by different tables with the primary key string that functions as the external key of the primary key for the superclass table.

In the case of the JOINED strategy, like in the case of the SINGLE TABLE strategy, the table to which the superclass is mapped must have an identification column.

Note
When using the JOINED strategy, binding must be executed multiple times during the generation of the subclass instances. Therefore, if the hierarchy structure becomes deep, the performance might deteriorate. Also, JOIN is necessary for issuing queries across the class hierarchy.