Chapter 8. Entity Inheritance

In order to be complete, an object to relational mapping engine must support inheritance hierarchies. The Java Persistence specification supports entity inheritance, polymorphic relationships/associations, and polymorphic queries. These features were completely missing in the older EJB CMP 2.1 specification.

In this chapter, we’ll modify our Customer entity that we defined in earlier chapters to make it fit into an inheritance hierarchy. We’ll have it extend a base class called Person and define an Employee class that extends a Customer class. Employees will be treated with a special discount when making reservations with Titan Cruises. Figure 8-1 shows this class hierarchy.

Customer class hierarchy
Figure 8-1. Customer class hierarchy

The Java Persistence specification provides three different ways to map an inheritance hierarchy to a relational database:

A single table per class hierarchy

One table will have all properties of every class in the hierarchy.

A table per concrete class

Each class will have a table dedicated to it, with all of its properties and the properties of its superclass mapped to this table.

A table per subclass

Each class will have its own table. Each table will have only the properties that are defined in that particular class. These tables will not have properties of any superclass or subclass.

In this chapter, we’ll use these three strategies to map the Customer ...

Get Enterprise JavaBeans 3.0, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.