The Java Persistence Series

Java tip: Inheritance relationships in JPA and Hibernate

Choosing a JPA inheritance strategy that supports polymorphism

Inheritance is a common pattern in object-oriented programming, but it's not easily replicated in a database. This Java tip shows you how to model inheritance relationships in JPA with Hibernate.

Learn the pros and cons of four different ORM inheritance strategies and get tips for choosing the one that best supports your application needs.

The inheritance pattern in ORM

Inheritance is an object-oriented pattern in which one class extends (or specializes) another class in order to borrow (or inherit) some of its functionality. For example, let's say we have a class named Car that represents all cars, including a make, model, and year. But now we want to create some more specialized types of car, such as SportsCar and SportUtilityVehicle. These cars would have all the features of the original Car class, but with some additional functionality. A SportsCar might need special parameters for designating speed. A SportsUtilityVehicle might need flags for seating and towing capacity.

Databases have no notion of inheritance between entities, so JPA providers like Hibernate must provide special features for defining object-oriented inheritance relationships in the database.

To continue reading this article register now