How do you deal with polymorphism in a database?

Take a look at Martin Fowler’s Patterns of Enterprise Application Architecture:

  • Single Table Inheritance:

    When mapping to a relational database, we try to minimize the joins that can quickly mount up when processing an inheritance structure in multiple tables. Single Table Inheritance maps all fields of all classes of an inheritance structure into a single table.

  • Class Table Inheritance:

    You want database structures that map clearly to the objects and allow links anywhere in the inheritance structure. Class Table Inheritance supports this by using one database table per class in the inheritance structure.

  • Concrete Table Inheritance:

    Thinking of tables from an object instance point of view, a sensible route is to take each object in memory and map it to a single database row. This implies Concrete Table Inheritance, where there’s a table for each concrete class in the inheritance hierarchy.

Leave a Comment