Hibernate Inheritance

In this inheritance strategy, only one table is created for all the classes involved in the hierarchy with an additional column known as a discriminator column. The discriminator column helps indifferentiating between the two subclasses. A SINGLE_TABLE strategy is the default strategy.

Following are the annotations used in this strategy:

  1. @Inheritance– It defines the inheritance strategy used in the application. This annotation is specified on the entity class that is the root of all the entity classes.
  • @DiscriminatorColumn- It specifies the discriminator column for SINGLE_TABLE and JOINED inheritance strategies.
  • @DiscriminatorValue- Discriminator values are the values of the @DiscriminatorColumn. This annotation is used to specify the values of the discriminator column for the given type entity.

Syntax of SINGLE_TABLE Inheritance

If @Inheritance annotation is not specified or no strategy is specified, it will consider SINGLE_TABLE strategy by default.

Example of SINGLE_TABLE Inheritance

In this example, we are taking three classes; i.e., Payment.java, Card.java, and Cheque.java.

The class- hierarchy is given below:-

Example of SINGLE_TABLE Inheritance
  1. Create all POJO classes.

In this step, we are going to create all the persistent classes, i.e., Payment.java, Card.java, and Cheque.java.




  • Create the Configuration file.

The configuration file contains the information of mapping classes and database. We will map all the persistent classes in the configuration file (hibernate.cfg.xml).


  • Create the main class that stores the object of Persistent object

In this step, we are going to create a main class (which contains the main method) that stores the object of the persistent class.


Hibernate Inheritance - SINGLE TABLE
DATABASE TABLE Hibernate Inheritance


The main disadvantages of this strategy are:

  • Most of the values are null in the table; hence, we cannot apply not null constraint.
  • In this strategy, the table cannot be normalized, which means we cannot expand this table into new tables or columns.

To overcome these disadvantages, we use TABLE_PER_CLASS strategy.

Pin It on Pinterest

Share This