Inheritance Mapping Strategies
What you'll learn: How to map object-oriented class hierarchies to relational database tables using three fundamental approaches.
When your domain model contains inheritance (a Manager extends Employee, for example), you face a challenge: relational databases don't have native inheritance support. You need a strategy to flatten your class hierarchy into tables.
The Three Main Strategies
Single Table Inheritance
All classes in the hierarchy share one table with columns for every possible field across all subclasses. A discriminator column (like employee_type) identifies which class each row represents. Unused columns remain NULL.
Analogy: One filing cabinet with folders that have different sections—some sections stay empty depending on the document type.
Trade-offs: Fast queries (no joins), but wastes space and can violate normalization principles.
Class Table Inheritance
Each class gets its own table. The base class table holds common fields; subclass tables hold specialized fields and foreign keys back to the parent. Retrieving a complete object requires joining tables.
Analogy: Separate filing cabinets linked by reference numbers—you pull multiple folders to get the complete record.
Trade-offs: Normalized and space-efficient, but queries become slower with multiple joins.
Concrete Table Inheritance
Each concrete class gets a complete, standalone table with all inherited and unique fields. Abstract base classes don't get tables.
Analogy: Completely independent filing cabinets with duplicated common information in each.
Trade-offs: No joins needed, but duplicate schema across tables and difficult polymorphic queries (how do you query "all employees" when they're in separate tables?).
Choosing Your Strategy
Consider query patterns, hierarchy depth, and polymorphism needs. Single-table works for shallow hierarchies; class-table fits normalized designs; concrete-table suits when you rarely query polymorphically.
Key Takeaway: Inheritance mapping strategies solve the impedance mismatch between object hierarchies and flat tables—choose single-table for simplicity, class-table for normalization, or concrete-table for query independence.