Show TOC

InheritanceLocate this document in the navigation structure

Use

Since entities are regular Java objects, they can inherit from other entities or from other Java classes that are not entities (mapped superclasses). Such an inheritance hierarchy is called “entity hierarchy” .

JPA specifies three inheritance strategies:

  • Single table per class hierarchy

  • Joined subclass

  • Table per concrete class

Note

The SAP implementation of JPA supports the single table per hierarchy strategy and the joined subclass strategy only.

Single Table per Class Hierarchy

This inheritance mapping strategy uses a single table to which all of the classes within a hierarchy are mapped. The table has a “discriminator column” that stores values that refer to the particular subclass of the row instance. The columns that correspond to subclass-specific states must be null-able. The JPA implementation by SAP supports this strategy.

Example

The following table represents the “Single Table per Class Hierarchy” inheritance mapping strategy. In this example, the discriminator column is EMPLOYEE_TYPE :

Sample Code
@Entity
@Table(name="EMPLOYEE")
@Inheritance
@DiscriminatorColumn(name="EMPLOYEE_TYPE")
public abstract class Employee {
        // ...
}

@Entity(name="PART_TIME")
public class PartTimeEmployee extends Employee {
        // ...
}

@MappedSuperclass
public abstract class CompanyEmployee extends Employee {
        // ...
}

@Entity
@DiscriminatorValue("FULL_TIME")
public class FullTimeEmployee extends CompanyEmployee {
        // ...
}
               
Example

ID

FIRSTNAME

LASTNAME

ADDRESS

DEPARTMENT

EMPLOYEE_TYPE

1

FIRSTNAME_1

LASTNAME_1

ADDRESS_1

DEPARTMENT_1

PART_TIME

2

FIRSTNAME_2

LASTNAME_2

ADDRESS_2

DEPARTMENT_2

FULL_TIME

3

FIRSTNAME_3

LASTNAME_3

ADDRESS_3

DEPARTMENT_1

FULL_TIME

Joined Subclass

The joined subclass strategy represents the hierarchy by multiple tables. The superclass of the hierarchy containing the primary key columns, columns for all other attributes of the superclass, and additionally a “discriminator column” . The subclasses, on the other hand, are represented by separate tables that contain foreign key columns referring to the primary key of the table the superclass is mapped to, and additional columns for the subclasses' specific fields. The JPA implementation by SAP supports this strategy.

Example

Sample Code
@Entity
@Table(name="EMPLOYEE")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="EMP_TYPE", discriminatorType=DiscriminatorType.INTEGER)
public abstract class Employee {
        // ...
}

@Entity
@Table(name="PART_TIME")
@DiscriminatorValue("1")
public class PartTimeEmployee extends Employee {
        // ...
}

@MappedSuperclass
public abstract class CompanyEmployee extends Employee {
        // ...
}
@Entity
@Table(name="FULL_TIME")
@DiscriminatorValue("2")
public class FullTimeEmployee extends CompanyEmployee {
        // ...
}
               

Table per Concrete Class

This strategy works on the basis of mapping each class from the hierarchy to a separate table. Both declared and inherited properties of the classes are mapped to columns of the corresponding tables. The JPA implementation by SAP does not support this strategy.