title | shortTitle | description | category | language | tag | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Table Inheritance Pattern in Java: Modeling Hierarchical Data in Relational Databases |
Table Inheritance |
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. |
Data Access Pattern, Structural Pattern |
en |
|
- Class Table Inheritance
The Table Inheritance pattern models a class hierarchy in a relational database by creating separate tables for each class in the hierarchy. These tables share a common primary key, which in subclass tables also serves as a foreign key referencing the primary key of the base class table. This linkage maintains relationships and effectively represents the inheritance structure. This pattern enables the organization of complex data models, particularly when subclasses have unique properties that must be stored in distinct tables.
Consider a Vehicle Management System with a Vehicle
superclass and subclasses like Car
and Truck
.
- The Vehicle Table stores attributes common to all vehicles, such as
make
,model
, andyear
. Its primary key (id
) uniquely identifies each vehicle. - The Car Table and Truck Table store attributes specific to their respective types, such as
numberOfDoors
for cars andpayloadCapacity
for trucks. - The
id
column in the Car Table and Truck Table serves as both the primary key for those tables and a foreign key referencing theid
in the Vehicle Table.
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.
In table inheritance, each class in the hierarchy is represented by a separate table, which allows for a clear distinction between shared attributes (stored in the base class table) and specific attributes (stored in subclass tables).
Relational databases don't support inheritance, which creates a mismatch when mapping objects. To fix this, Table Inheritance uses a separate table for each class in the hierarchy while maintaining relationships through foreign keys, making it easier to link the classes together in the database.
For more detailed information, refer to Martin Fowler's article on Class Table Inheritance.
The Vehicle
class will be the superclass, and we will have Car
and Truck
as subclasses that extend
Vehicle
. The Vehicle
class will store common attributes, while Car
and Truck
will store
attributes specific to those subclasses.
-
Superclass (
Vehicle
):
TheVehicle
class stores attributes shared by all vehicle types, such as:make
: The manufacturer of the vehicle.model
: The model of the vehicle.year
: The year the vehicle was manufactured.id
: A unique identifier for the vehicle.
These attributes are stored in the
Vehicle
table in the database. -
Subclass (
Car
andTruck
):
Each subclass (Car
andTruck
) stores attributes specific to that vehicle type:Car
: Has an additional attributenumberOfDoors
representing the number of doors the car has.Truck
: Has an additional attributepayloadCapacity
representing the payload capacity of the truck.
These subclass-specific attributes are stored in the
Car
andTruck
tables. -
Foreign Key Relationship:
Each subclass (Car
andTruck
) contains theid
field which acts as a foreign key that references the primary key (id
) of the superclass (Vehicle
). This foreign key ensures the relationship between the common attributes in theVehicle
table and the specific attributes in the subclass tables (Car
andTruck
).
/**
* Superclass
* Represents a generic vehicle with basic attributes like make, model, year, and ID.
*/
public class Vehicle {
private String make;
private String model;
private int year;
private int id;
// Constructor, getters, and setters...
}
/**
* Represents a car, which is a subclass of Vehicle.
*/
public class Car extends Vehicle {
private int numberOfDoors;
// Constructor, getters, and setters...
}
/**
* Represents a truck, which is a subclass of Vehicle.
*/
public class Truck extends Vehicle {
private int payloadCapacity;
// Constructor, getters, and setters...
}
Column | Description |
---|---|
id | Primary key |
make | The make of the vehicle |
model | The model of the vehicle |
year | The manufacturing year of the vehicle |
Column | Description |
---|---|
id | Foreign key referencing Vehicle(id) |
numberOfDoors | Number of doors in the car |
Column | Description |
---|---|
id | Foreign key referencing Vehicle(id) |
payloadCapacity | Payload capacity of the truck |
- When your application requires a clear mapping of an object-oriented class hierarchy to relational tables.
- When subclasses have unique attributes that do not fit into a single base table.
- When scalability and normalization of data are important considerations.
- When you need to separate concerns and organize data in a way that each subclass has its own table but maintains relationships with the superclass.
- 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
. - 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
.
- Clear Structure: Each class has its own table, making the data model easier to maintain and understand.
- Scalability: Each subclass can be extended independently without affecting the other tables, making the system more scalable.
- Data Normalization: Helps avoid data redundancy and keeps the schema normalized.
- Multiple Joins: Retrieving data that spans multiple subclasses may require joining multiple tables, which could lead to performance issues.
- Increased Complexity: Managing relationships between tables and maintaining integrity can become more complex.
- Potential for Sparse Tables: Subclasses with fewer attributes may end up with tables that have many null fields.
- Single Table Inheritance – A strategy where a single table is used to store all classes in an inheritance hierarchy. It stores all attributes of the class and its subclasses in one table.
- Singleton Pattern – Used when a class needs to have only one instance.
- Martin Fowler - Patterns of Enterprise Application Architecture
- Java Persistence with Hibernate - Link to book
- Object-Relational Mapping on Wikipedia - Link to article