Single Table Inheritance Strategy

The Single Table Inheritance strategy states that all the entities in this hierarchy are mapped to a single table in the database. Separate columns are used to differentiate between multiple entities. This strategy is suitable when entities in the hierarchy share most of their attributes and have a small number of different fields. 

Example for Single Table Inheritance Strategy:

Java




// on the below line creating an entity for Student
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "entity_type",
                     discriminatorType
                     = DiscriminatorType.STRING)
public class Student {
    // on the below line creating a field for id which we
    // are annotating with @Id.
    @Id 
      private int id;
    // on the below line creating a field for the student
    // name.
    private String studentName;
}
// on the below line creating a separate entity for boys in
// the group of students.
@Entity
// on the below line specifying the discriminator value as
// boy.
@DiscriminatorValue("BOY")
// on the below line creating a class for boy and extending
// it with Student
public class Boy extends Student {
    // Define Boys-specific properties in this class.
}
@Entity
// on the below line specifying the discriminator value as
// girl.
@DiscriminatorValue("GIRL")
// on the below line creating a class for girl and extending
// it with Student
public class Girl extends Student {
    // Define Girls-specific properties in this class.
}


Code Explanation:

In the above example, we are creating a Base class for Students which will serve for both boys as well as girls. Inside this class we are specifying the common properties such as id, name. We are adding an inheritance annotation for the Student base class as InheritanceType.SINGLE_TABLE which indicates the Single Table inheritance strategy. The discriminator column entity_type is used to differentiate between entities and it is specified with @DiscriminatorColumn annotation. The discriminator values for each entity are set using @DiscriminatorValue annotation. 

Hibernate – @Inheritance Annotation

The @Inheritance annotation in JPA is used to specify the inheritance relation between two entities. It is used to define how the data of the entities in the hierarchy should be stored in the database. The @Inheritance  annotation provides us with benefits to reduce code complexity by creating a base class and inheriting other classes from the base class. The @Inheritance annotation is applied to the root entity class to define the inheritance strategy. There are different types of strategies available for Inheritance annotation which are as follows: 

  1. Single Table Inheritance Strategy. 
  2. Joined Inheritance Strategy. 
  3. Table Per Class Inheritance Strategy. 

If no strategy is defined while using @inheritance annotation the default Single Table Inheritance Strategy is applied. 

Similar Reads

1. Single Table Inheritance Strategy

The Single Table Inheritance strategy states that all the entities in this hierarchy are mapped to a single table in the database. Separate columns are used to differentiate between multiple entities. This strategy is suitable when entities in the hierarchy share most of their attributes and have a small number of different fields....

2. Joined Inheritance Strategy

...

3. Table Per Class Inheritance Strategy

The Joined Inheritance Strategy generates a separate table for each entity class. The attribute for each table is joined with the primary key. It removes the possibility of duplicity. Each table that is being created contains specific columns and a join operation is used to retrieve data from multiple tables when querying the entire hierarchy. We can use the strategy when the entities we are creating have differences in their properties and relationships....