Hello friends,

Welcome back, today we will discuss Is-A relationship in hibernate. It is a very important and initial topic of hibernate.

I hope that now you can understand basic approach of hibernate, if not, then you can please follow my previous tutorials. Before starting this session,  I am expecting you to know to create the basic program using hibernate.

This tutorial is only based on configuration. So let’s start the journey.

We can configure the inheritance of the hierarchy classes with the table of the database. Hibernate supports three inheritance mapping strategies defined:-

a) Table Per Hierarchy

b) Table Per Subclass

c)Table Per Concrete class

Let see the example of the relation between the classes:-

 

Let person-type be the discriminator column and following be its value:-

 

1) Table per hierarchy:-

In this approach entity of all these classes are mapped into a single table. This table contains columns for all the data member of all the classes. In additional column is called discriminator.

Discriminator:-
The column is used to identify which records of the table represents the object of which class.

Advantage:- The main advantage of this approach is performance because hibernate needs to manage only one table for the entire family.
Disadvantage:- a) Table is not normalized.
b) A not null constant can not be applied to the data members of subclasses.

Following mapping are required for this approach.

<class name=”Super class name” table=”table name*”>
<id name=”unique identifier” type=”datatype” column=”column name*”>
<generator class=”generator name”/>
</id>
<discriminator column=”column name” type=”daa_a_a_tatype”>
<property name=”property name” type=”datatype” column=”column name*”/>
…….
<subclass name=”subclass name” discriminator-value=”value”>
<property name=”property name” type=”datatype” column=”column name*”/>
……
</subclass>
…….
</class>

Note :- * represent optional.

2 )Table Per Subclass:-

In this approach, a separate table is created for each class. Each table contains column to store only the declared data member of the class.
All the tables are of the subclasses are joined to the superclass table by using the primary key of the superclass table as the foreign key in them.

Advantage:-

  • Tables are normalized
  • A not null constant can be applied to the columns which are mapped to subclass data member.

Disadvantage:-

  • Performance is degraded because hibernate needs to execute multiple queries.

Following mapping are required for that approach:-

<class name=”Super class name” table=”table name*”>
<id name=”unique identifier” type=”datatype” column=”column name*”>
<generator class=”generator name”/>
</id>
<property name=”property name” type=”datatype” column=”column name*”/>
……..
<joined-subclass name=”subclass name”>
<key column=”column to recive pk of super class table as fk”/>
<property name=”property name” type=”datatype” column=”column name*”/>
………
</joined-subclass>
…….
</class>

 

Note :- * represent optional

3) Table Per Concrete class:-

In this approach, a separate table is created for each class. Each table contains columns for the declared as well as an inherited data member of the mapped class.

Advantage:-

  • Better performance is obtained as compared to the previous approach because hibernate need to manage only one table for each class.

Disadvantage:-

  • When we use the same strategy then only the states of the entity is saved and their relation between classes are lost.
  • This hierarchy is not recommended for most cases.
  • Changes to a parent class is reflected in a large number of tables.

Note:- For this reason, this approach is not supported by all the ORM frameworks.

<class name=”Super class name” table=”table name*”>
<id name=”unique identifier” type=”datatype” column=”column name*”>
<generator class=”generator name”/>
</id>
<property name=”property name” type=”datatype” column=”column name*”/>
……..
<union-subclass name=”subclass name”>
<property name=”property name” type=”datatype” column=”column name*”/>
………
</union-subclass>
…….
</class>

 

Thanks, happy learning.