|
| 1 | +--- |
| 2 | +title: "Table Inheritance Pattern in Java: Modeling Hierarchical Data in Relational Databases" |
| 3 | +shortTitle: Table Inheritance |
| 4 | +description: "Explore the Table Inheritance pattern in Java with real-world examples, database schema, and tutorials. Learn how to model class hierarchies elegantly in relational databases." |
| 5 | +category: Data Access Pattern, Structural Pattern |
| 6 | +language: en |
| 7 | +tag: |
| 8 | +- Decoupling |
| 9 | +- Inheritance |
| 10 | +- Polymorphism |
| 11 | +- Object Mapping |
| 12 | +- Persistence |
| 13 | +- Data Transformation |
| 14 | +--- |
| 15 | + |
| 16 | +## Also Known As |
| 17 | +- Class Table Inheritance |
| 18 | +--- |
| 19 | + |
| 20 | +## Intent of Table Inheritance Pattern |
| 21 | +The Table Inheritance pattern models a class hierarchy in a relational database by creating |
| 22 | +separate tables for each class in the hierarchy. These tables share a common primary key, which in |
| 23 | +subclass tables also serves as a foreign key referencing the primary key of the base class table. |
| 24 | +This linkage maintains relationships and effectively represents the inheritance structure. This pattern |
| 25 | +enables the organization of complex data models, particularly when subclasses have unique properties |
| 26 | +that must be stored in distinct tables. |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## Detailed Explanation of Table Inheritance Pattern with Real-World Examples |
| 31 | + |
| 32 | +### Real-World Example |
| 33 | +Consider a **Vehicle Management System** with a `Vehicle` superclass and subclasses like `Car` and `Truck`. |
| 34 | + |
| 35 | +- The **Vehicle Table** stores attributes common to all vehicles, such as `make`, `model`, and `year`. Its primary key (`id`) uniquely identifies each vehicle. |
| 36 | +- The **Car Table** and **Truck Table** store attributes specific to their respective types, such as `numberOfDoors` for cars and `payloadCapacity` for trucks. |
| 37 | +- The `id` column in the **Car Table** and **Truck Table** serves as both the primary key for those tables and a foreign key referencing the `id` in the **Vehicle Table**. |
| 38 | + |
| 39 | +This setup ensures each subclass entry corresponds to a base class entry, maintaining the inheritance relationship while keeping subclass-specific data in their own tables. |
| 40 | + |
| 41 | +### In Plain Words |
| 42 | +In table inheritance, each class in the hierarchy is represented by a separate table, which |
| 43 | +allows for a clear distinction between shared attributes (stored in the base class table) and |
| 44 | +specific attributes (stored in subclass tables). |
| 45 | + |
| 46 | +### Martin Fowler Says |
| 47 | + |
| 48 | +Relational databases don't support inheritance, which creates a mismatch when mapping objects. |
| 49 | +To fix this, Table Inheritance uses a separate table for each class in the hierarchy while maintaining |
| 50 | +relationships through foreign keys, making it easier to link the classes together in the database. |
| 51 | + |
| 52 | +For more detailed information, refer to Martin Fowler's article on [Class Table Inheritance](https://martinfowler.com/eaaCatalog/classTableInheritance.html). |
| 53 | + |
| 54 | + |
| 55 | +## Programmatic Example of Table Inheritance Pattern in Java |
| 56 | + |
| 57 | + |
| 58 | +The `Vehicle` class will be the superclass, and we will have `Car` and `Truck` as subclasses that extend |
| 59 | +`Vehicle`. The `Vehicle` class will store common attributes, while `Car` and `Truck` will store |
| 60 | +attributes specific to those subclasses. |
| 61 | + |
| 62 | +### Key Aspects of the Pattern: |
| 63 | + |
| 64 | +1. **Superclass (`Vehicle`)**: |
| 65 | + The `Vehicle` class stores attributes shared by all vehicle types, such as: |
| 66 | + - `make`: The manufacturer of the vehicle. |
| 67 | + - `model`: The model of the vehicle. |
| 68 | + - `year`: The year the vehicle was manufactured. |
| 69 | + - `id`: A unique identifier for the vehicle. |
| 70 | + |
| 71 | + These attributes are stored in the **`Vehicle` table** in the database. |
| 72 | + |
| 73 | +2. **Subclass (`Car` and `Truck`)**: |
| 74 | + Each subclass (`Car` and `Truck`) stores attributes specific to that vehicle type: |
| 75 | + - `Car`: Has an additional attribute `numberOfDoors` representing the number of doors the car has. |
| 76 | + - `Truck`: Has an additional attribute `payloadCapacity` representing the payload capacity of the truck. |
| 77 | + |
| 78 | + These subclass-specific attributes are stored in the **`Car` and `Truck` tables**. |
| 79 | + |
| 80 | +3. **Foreign Key Relationship**: |
| 81 | + Each subclass (`Car` and `Truck`) contains the `id` field which acts as a **foreign key** that |
| 82 | +references the primary key (`id`) of the superclass (`Vehicle`). This foreign key ensures the |
| 83 | +relationship between the common attributes in the `Vehicle` table and the specific attributes in the |
| 84 | +subclass tables (`Car` and `Truck`). |
| 85 | + |
| 86 | + |
| 87 | +```java |
| 88 | +/** |
| 89 | + * Superclass |
| 90 | + * Represents a generic vehicle with basic attributes like make, model, year, and ID. |
| 91 | + */ |
| 92 | +public class Vehicle { |
| 93 | + private String make; |
| 94 | + private String model; |
| 95 | + private int year; |
| 96 | + private int id; |
| 97 | + |
| 98 | + // Constructor, getters, and setters... |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Represents a car, which is a subclass of Vehicle. |
| 103 | + */ |
| 104 | +public class Car extends Vehicle { |
| 105 | + private int numberOfDoors; |
| 106 | + |
| 107 | + // Constructor, getters, and setters... |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Represents a truck, which is a subclass of Vehicle. |
| 112 | + */ |
| 113 | +public class Truck extends Vehicle { |
| 114 | + private int payloadCapacity; |
| 115 | + |
| 116 | + // Constructor, getters, and setters... |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +## Table Inheritance Pattern Class Diagram |
| 123 | + |
| 124 | + |
| 125 | +<img src="etc/class-diagram.png" width="400" height="500" /> |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +## Table Inheritance Pattern Database Schema |
| 133 | + |
| 134 | +### Vehicle Table |
| 135 | +| Column | Description | |
| 136 | +|--------|-------------------------------------| |
| 137 | +| id | Primary key | |
| 138 | +| make | The make of the vehicle | |
| 139 | +| model | The model of the vehicle | |
| 140 | +| year | The manufacturing year of the vehicle | |
| 141 | + |
| 142 | +### Car Table |
| 143 | +| Column | Description | |
| 144 | +|------------------|-------------------------------------| |
| 145 | +| id | Foreign key referencing `Vehicle(id)` | |
| 146 | +| numberOfDoors | Number of doors in the car | |
| 147 | + |
| 148 | +### Truck Table |
| 149 | +| Column | Description | |
| 150 | +|-------------------|-------------------------------------| |
| 151 | +| id | Foreign key referencing `Vehicle(id)` | |
| 152 | +| payloadCapacity | Payload capacity of the truck | |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +## When to Use the Table Inheritance Pattern in Java |
| 157 | + |
| 158 | +- When your application requires a clear mapping of an object-oriented class hierarchy to relational tables. |
| 159 | +- When subclasses have unique attributes that do not fit into a single base table. |
| 160 | +- When scalability and normalization of data are important considerations. |
| 161 | +- When you need to separate concerns and organize data in a way that each subclass has its own |
| 162 | +table but maintains relationships with the superclass. |
| 163 | + |
| 164 | +## Table Inheritance Pattern Java Tutorials |
| 165 | + |
| 166 | +- [Software Patterns Lexicon: Class Table Inheritance](https://softwarepatternslexicon.com/patterns-sql/4/4/2/) |
| 167 | +- [Martin Fowler: Class Table Inheritance](http://thierryroussel.free.fr/java/books/martinfowler/www.martinfowler.com/isa/classTableInheritance.html) |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## Real-World Applications of Table Inheritance Pattern in Java |
| 172 | + |
| 173 | +- **Vehicle Management System**: Used to store different types of vehicles like Car and Truck in separate tables but maintain a relationship through a common superclass `Vehicle`. |
| 174 | +- **E-Commerce Platforms**: Where different product types, such as Clothing, Electronics, and Furniture, are stored in separate tables with shared attributes in a superclass `Product`. |
| 175 | + |
| 176 | +## Benefits and Trade-offs of Table Inheritance Pattern |
| 177 | + |
| 178 | +### Benefits |
| 179 | + |
| 180 | +- **Clear Structure**: Each class has its own table, making the data model easier to maintain and understand. |
| 181 | +- **Scalability**: Each subclass can be extended independently without affecting the other tables, making the system more scalable. |
| 182 | +- **Data Normalization**: Helps avoid data redundancy and keeps the schema normalized. |
| 183 | + |
| 184 | +### Trade-offs |
| 185 | + |
| 186 | +- **Multiple Joins**: Retrieving data that spans multiple subclasses may require joining multiple tables, which could lead to performance issues. |
| 187 | +- **Increased Complexity**: Managing relationships between tables and maintaining integrity can become more complex. |
| 188 | +- **Potential for Sparse Tables**: Subclasses with fewer attributes may end up with tables that have many null fields. |
| 189 | + |
| 190 | +## Related Java Design Patterns |
| 191 | + |
| 192 | +- **Single Table Inheritance** – A strategy where a single table is used to store all classes in an |
| 193 | +inheritance hierarchy. It stores all attributes of the class and its subclasses in one table. |
| 194 | +- **Singleton Pattern** – Used when a class needs to have only one instance. |
| 195 | + |
| 196 | + |
| 197 | +## References and Credits |
| 198 | + |
| 199 | +- **Martin Fowler** - [*Patterns of Enterprise Application Architecture*](https://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420) |
| 200 | +- **Java Persistence with Hibernate** - [Link to book](https://www.amazon.com/Java-Persistence-Hibernate-Christian-Bauer/dp/193239469X) |
| 201 | +- **Object-Relational Mapping on Wikipedia** - [Link to article](https://en.wikipedia.org/wiki/Object-relational_mapping) |
0 commit comments