Inheritance Mapping in Hibernate Tutorial with examples

January 18, 2015 0 Comments

Compared to JDBC we have one main advantage in hibernate, which is hibernate inheritance.

Inheritance is one of the most visible facets of Object-relational mismatch. Object oriented systems can model both “is a” and “has a” relationship. Relational model supports only “has a” relationship between two entities. Hibernate can help you map such Objects with relational tables. But you need to choose certain mapping strategy based on your needs.

Inheritance Mapping Strategies in Hibernate

Hibernate supports 3 types of Inheritance Mappings:
  1. Table per class hierarchy
  2. Table per sub-class hierarchy
  3. Table per concrete class hierarchy
Note: We can also called this Hibernate Inheritance Mapping as Hibernate Hierarchy

Table Per Hierarchy

In table per hierarchy mapping, single table is required to map the whole hierarchy, an extra column (known as discriminator column) is added to identify the class. But nullable values are stored in the table .

Table Per Concrete class

In case of table per concrete class, tables are created as per class. But duplicate column is added in subclass tables.

Table Per Subclass

In this strategy, tables are created as per class but related by foreign key. So there are no duplicate columns.
Inheritance Mapping in Hibernate Tutorial with examples

Prakash Hari Sharma
I'm a software developer and Java enthusiast. I like clean and modular code, enjoy Agile projects and have a passion for trying out new things. I try to learn and teach every day, and occasionally I'm even able to blog about it. :)