Have just put an example on Github of how to handle inheritance and polymorphism with Hibernate:
It demonstrates the four ways of dealing with inheritance and polymorphism:
- Implicit polymorphism – no explicit mapping of the inheritance, but Hibernate can support polymorphic queries because it understands the class hierarchy.
- Table per concrete subclass – abstract class at the top of the hierarchy does not correspond to a database table, meaning that any properties in the superclass are duplicated into each subclass table, and polymorphic queries have to use unions.
- Table per class. All classes, including abstract ones, get a database table, meaning that no fields are duplicated. Better from a normalization point of view, but queries can still be slow because they have to perform joins.
- Single table for entire class hierarchy. Fast, because no joins or unions required for any queries, but not good from a database normalization perspective, because subclass columns have to be nullable.
The project uses maven, TestNG and HSQLDB. Hence, to run the tests, use:
For more information on Hibernate and polymorphism, see chapter ten of the Hibernate docs: