Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Advanced Inheritance Mapping in Hibernate

Introduction to Inheritance Mapping

Inheritance mapping in Hibernate is a way to implement the Object-Oriented Programming principle of inheritance in a relational database. It allows you to define a base class and its subclasses, mapping them to a single table or multiple tables in the database.

In this tutorial, we will explore advanced techniques for inheritance mapping, focusing on various strategies such as Single Table, Joined Table, and Table Per Class.

Single Table Inheritance

Single Table Inheritance is a strategy where all classes in the inheritance hierarchy are mapped to a single table. A discriminator column is used to differentiate the types of entities.

Example

Consider an example of a Vehicle hierarchy:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "vehicle_type")
public abstract class Vehicle {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String make;
    private String model;
}

@Entity
@DiscriminatorValue("Car")
public class Car extends Vehicle {
    private int numberOfDoors;
}

@Entity
@DiscriminatorValue("Truck")
public class Truck extends Vehicle {
    private double payloadCapacity;
}
                    

This example shows a single table for Vehicle, Car, and Truck with a discriminator column called vehicle_type.

Joined Table Inheritance

Joined Table Inheritance creates a separate table for each class in the hierarchy. The base class table contains the common attributes, while the subclass tables contain specific attributes.

Example

Using the same Vehicle hierarchy, the mapping would look like this:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Vehicle {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String make;
    private String model;
}

@Entity
public class Car extends Vehicle {
    private int numberOfDoors;
}

@Entity
public class Truck extends Vehicle {
    private double payloadCapacity;
}
                    

In this case, the Vehicle table will contain common attributes, and the Car and Truck tables will contain their specific attributes.

Table Per Class Inheritance

Table Per Class Inheritance creates a separate table for each class in the hierarchy, including the base class. This means that each table contains all of its own attributes, including those inherited from the parent class.

Example

The Vehicle hierarchy using Table Per Class would look like this:

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

@Entity
public class Car extends Vehicle {
    private int numberOfDoors;
}

@Entity
public class Truck extends Vehicle {
    private double payloadCapacity;
}
                    

Each class has its own table with all attributes, which can lead to data redundancy but offers better performance for certain queries.

Conclusion

In this tutorial, we explored advanced inheritance mapping techniques in Hibernate. Each strategy has its own advantages and trade-offs, and the choice of strategy depends on the specific requirements of your application.

Understanding these strategies allows you to design your database schema more effectively and leverage the power of Hibernate for managing inheritance hierarchies.