Inheritance Hierarchical Mapping Tutorial Hibernate

Inheritance Hierarchical Mapping Tutorial Hibernate

Inheritance Hierarchical Mapping

Java, being an OOPs language, supports inheritance for reusability of classes. Two types of reusability exists – "is-a" and "has-a" relationship. The relational model supported by Hibernate is "has-a" relationship. How Hibernate writes tables for the Java classes involved in inheritance?

Hibernate comes with a provision to create tables and populate them as per the Java classes involved in inheritance. Hibernate offers basically 3 different approaches to map hierarchical classes (classes involved in inheritance) with database tables. Here, we have classes but no tables. Tables are created by Hibernate automatically. There are 3 styles to do this job. Explained in this tutorial "Inheritance Hierarchical Mapping".

Inheritance Hierarchical Mapping

Observe, in the above hierarchy, three classes are involved where Employee is the super class and PermanentEmployee and TemporaryEmployee are subclasses with their own properties declared as instance variables. Now the question is how many tables are required and moreover how to link the tables so that PermanentEmployee gets four properties of empId and empName (from super class), designation and department.

The three approaches adopted by Hibernate are

  1. table-per-class-hierarchy: Only one table is created for all the classes involved in hierarchy. Here we maintain an extra discriminator field in table to differentiate between PermanentEmployee and TemporaryEmployee.
  2. table-per-subclass: One table for each class is created. The above hierarchy gets three tables. Here, foreign key is maintained between the tables.
  3. table-per-concrete-class: One table for each concrete class (subclass) is created but not of super class. The above hierarchy gets two tables. As a special case, the super class can be an abstract or interface. Here, foreign key is not maintained.

The 1st strategy table-per-class hierarchy creates one table like the following with discriminator column.

Inheritance Hierarchical Mapping

The 2nd strategy table-per-subclass creates three tables like following where super class primary key is subclass foreign key.

Inheritance Hierarchical Mapping

PID and TID are the foreign keys for EMPID of super class. The relation should be given in the mapping file.

In the 3rd strategy table-per-concrete-class, only two tables for subclasses are created but not of super class. In the tables, the super class fields are also included.

Inheritance Hierarchical Mapping

For table-per-class hierarchy we use <subclass>, for table-per-subclass we use <joined- subclass> and for table-per-concrete-class we use <union-subclass< in mapping file to maintain the relation between super class and subclass.

Let us explore each of the above separately.

Hibernate (Hierarchical) Inheritance – Total Three Strategies – Three Programs

1. Table Per Class Example
2. Table Per Subclass Example
3. Table Per Concreteclass Example

One thought on “Inheritance Hierarchical Mapping Tutorial Hibernate

  1. Raj Gopal

    Hibernate represents “is-a” relationship .

    In java inheritance represents – “is a” relationship
    Aggregation represents – “Has a” relationship

Leave a Reply

Your email address will not be published. Required fields are marked *