Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Table Per Subclass in Hibernate

Introduction

In Hibernate, one of the inheritance mapping strategies is the "Table Per Subclass" approach. This method allows you to create a separate table for each subclass in your class hierarchy. Each subclass table contains only the fields that are specific to that subclass, along with a foreign key linking to the superclass table.

Concept Overview

The "Table Per Subclass" strategy is particularly useful when dealing with complex class hierarchies where each subclass has its own unique attributes. This approach maintains a clear separation of concerns, allowing each subclass to have its specific schema while still being linked to a common superclass.

For example, consider a class hierarchy where we have a superclass called Vehicle and its subclasses Car and Truck. The Vehicle class might contain common attributes like id and make, while Car could have numberOfDoors and Truck might have payloadCapacity.

Entity Class Definitions

Below are the entity class definitions for the superclass and subclasses:

Vehicle.java

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Vehicle {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String make;
    // Getters and Setters
}
                

Car.java

@Entity
public class Car extends Vehicle {
    private int numberOfDoors;
    // Getters and Setters
}
                

Truck.java

@Entity
public class Truck extends Vehicle {
    private double payloadCapacity;
    // Getters and Setters
}
                

Database Table Structure

When using the "Table Per Subclass" strategy, Hibernate creates separate tables for the superclass and each subclass. The structure would look something like this:

Table: Vehicle
| id | make     |
|----|----------|
| 1  | Toyota   |
| 2  | Ford     |

Table: Car
| id | make     | numberOfDoors |
|----|----------|----------------|
| 3  | Honda    | 4              |

Table: Truck
| id | make     | payloadCapacity |
|----|----------|-----------------|
| 4  | Volvo    | 1000.5          |
                

Advantages and Disadvantages

Advantages

  • Clear separation of attributes specific to each subclass.
  • Flexibility to modify the subclass schemas independently.
  • Queries can be more straightforward since each subclass is in its own table.

Disadvantages

  • Can lead to data redundancy and inconsistency if not managed correctly.
  • Queries involving the superclass may require joining multiple tables, leading to complex SQL queries.

Conclusion

The "Table Per Subclass" inheritance mapping strategy in Hibernate is a powerful tool for managing class hierarchies in a database. It offers a clear and organized way of mapping subclasses to their respective tables, enabling the use of object-oriented programming principles within relational databases. While it has its pros and cons, it can be the right choice depending on the specific requirements of your application.