Inheritance strategy in Hibernate

Hibernate comes with a provision to create tables and populate them as per the Java classes involved in inheritance. Hibernate offers basically three different approaches to map hierarchical classes – classes involved in inheritance with database tables.

Hibernate inheritance example

Observe, in the above hierarchy, three classes are involved where Person is the super class and Student and Teacher 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 Student gets three properties of id and name(from super class), year.

The three approaches adopted by Hibernate are

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 Student and Teacher.

For more information please go through table-per-class-hierarchy

table-per-subclass One table for each class is created. The above hierarchy gets three tables. Here, foreign key is maintained between the tables.

For more information please go through table-per-subclass

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.

For more information please go through table-per-concrete-class

That’s all. Thank your for your patience. Please do not forget to leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.