7 Mapping inheritance


This chapter covers

  • Examining inheritance-mapping strategies
  • Investigating polymorphic associations

We deliberately haven’t talked much about inheritance mapping so far. Mapping makes the connection between the object-oriented world and the relational world. Inheritance is specific only to object-oriented systems. Consequently, mapping a hierarchy of classes to tables can be a complex issue, and we demonstrate various strategies in this chapter.

A basic strategy for mapping classes to database tables might be “one table for every persistent entity class”. This approach sounds simple enough and indeed works well until we encounter inheritance.

Inheritance is such a visible structural mismatch between the object-oriented and relational worlds because the object-oriented systems model provides both is a and has a relationships. SQL-based models provide only has a relationships; SQL database management systems don’t support type inheritance—and even when it’s available, it’s usually proprietary or incomplete.

There are four different strategies for representing an inheritance hierarchy:

7.1 Table per concrete class with implicit polymorphism

7.2 Table per concrete class with unions

7.3 Table per class hierarchy

7.4 Table per subclass with joins

7.5 Mixing inheritance strategies

7.6 Inheritance of embeddable classes

7.7 Choosing a strategy

7.8 Polymorphic associations

7.8.1 Polymorphic many-to-one associations

7.8.2 Polymorphic collections

7.9 Summary