Table of Contents
Whenever an app has to manage the data of a larger number of object types, there may be various subtype (inheritance) relationships between some of the object types. Handling subtype relationships is an advanced issue in software application engineering. It is often not well supported by application development frameworks.
In this chapter of our tutorial, we explain two case studies based on fragments of the information model of our running example, the Public Library app, shown above.
In the first case study, we consider the single-level class hierarchy with root
Book
shown in Figure 18.1 below, which is an incomplete disjoint segmentation. We use the
Single Table Inheritance approach for mapping this class
hierarchy to a single database table.
In the second case study, we consider the multi-level class hierarchy consisting of the
Person
roles Employee
, Manager
and Author
,
shown in Figure 18.2 below. We
use the Joined Table Inheritance approach for mapping this
class hierarchy to a set of database tables that are related with each other via foreign key
dependencies.
In both cases we show
how to derive a JPA entity class model,
how to encode the JPA entity class model in the form of JPA entity classes (as model classes),
how to write the view and controller code based on the model code.
Java provides built-in support for subtyping with its extends
keyword, but it
does not support multiple inheritance. Consider the design
model shown in Figure 18.3 below.
First we define the superclass Person
. Then, we define the subclass
Student
and the subtyping relationship to Person
by means of the
extends
keyword:
public class Person { private String firstName; private String lastName; ... } public class Student extends Person { private int studentNo; public Student( String first, String last, int studNo) { super( firstName, lastName); this.setStudNo( studNo); } ... }
Notice that in the Student
class, we define a constructor with all the
parameters required to create an instance of Student
. In this subclass constructor
we use super
to invoke the constructor of the superclass Person
.