Differences between Table per class Table per subclass Table per concreteclass

Differences between Table per class Table per subclass Table per concreteclass


Table per class vs Table per subclass vs Table per concreteclass

To understand better, it is summarized all the differences between three hierarchical models of Hibernate.

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

Introduction notes for the above three models is available at Inheritance (Hierarchical) Mapping Tutorial

Table-per-class Table-per-subclass Table-per-concreteclass
No. Tables created for classes involved in hierarchy Only one table is created for all the classes One table for each class is created One table for each concrete class (subclass) is created but not of super classes
Extra Field/Key One Discriminator field is used Foreign key is maintained between the tables Foreign key is not maintained
Special feature Super class cannot be abstract or interface Super class cannot be abstract or interface Super class can be also an abstract or interface
XML mapping element <subclass> <joined- subclass> <union-subclass>
Polymorphic support between entities Provides good support Provides poor support Provides poor support
Null fields Some columns of subclasses will be null Need not be null Need not be null
Advantage Gives maximum performance even for the classes involved in deep hierarchy Changes in a single super class does not demand complex changes in database Simple style to implement inheritance mapping
Disadvantage Requires either columns to be added or removed when the members in the hierarchy change For deep hierarchy, it may give poor performance Members of super class are placed in each subclass and thereby any change in the super class should be reflected in each subclass

Limitations:

  1. Hibernate does not allow to write all the three elements <subclass>, <joined-subclass> and <union-subclass> mappings in one root <class> element.
  2. It is allowed to place both the table-per-class and table-per-subclass models under one <class> element.
  3. It is allowed to write all the three elements <subclass>, <joined-subclass> and <union-subclass> mappings in separate mapping files under one <hibernate-mapping>.
  4. In table-per-class, columns declared by the subclasses should not have the constraint NOT NULL.

One thought on “Differences between Table per class Table per subclass Table per concreteclass

Leave a Reply

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