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.
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.Tags: Hibernate inheritance • table per class hierarchy • table per concrete class • table per sub-class