Chapter 12. 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 the Employee 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 redefine the Employee class to extend a Customer class. Figure 12-1 shows this class hierarchy.

Customer class hierarchy

Figure 12-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 use these three strategies to map the Employee class hierarchy defined in Figure 12-1.

Single Table per Class Hierarchy

In the single table per ...

Get Enterprise JavaBeans 3.1, 6th 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.