Hibernate Inheritance Joined Table

In this inheritance strategy, mapping of the child class fields is done with the common fields of the parent class in a separate table. In other words, the common entities between the child classes and the parent class are mapped in the separate database table. To overcome the disadvantages of TABLE_PER_CLASS strategy, we are using JOINED table strategy. Hence, there is no repeated column present in the subclass tables.

Syntax of JOINED Table Inheritance

Here, InheritanceType defines the inheritance strategy we are using.

Example of JOINED table inheritance

Let’s understand this strategy by an example. Here, we are taking three classes that are Payment.java, Card.java, and Cheque.java.

The class hierarchy is given below:-

Hibernate Inheritance Joined Table

1) Create all POJO Classes

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

Payment.java

Card.java

Cheque.java

2) Create the configuration file.

The configuration file contains the information of mapping classes and database. We are going to map all the POJO classes in the configuration file (hibernate.cfg.xml).

hibernate.cfg.xml

3) Create the main class that stores the object of POJO object

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

App.java

OUTPUT

Hibernate Inheritance Joined Table 1

4. DATABASE TABLES

payment table

Hibernate Inheritance Joined Table 2

card table         

Hibernate Inheritance Joined Table 3

cheque table

Hibernate Inheritance Joined Table 4

The JOINED table is the best strategy among all the inheritance strategies. Following are the advantages of this strategy:

  • Repeated columns are not present in the database tables.
  • Nullable values are not present in the table.