Tuesday, 1 July 2025

Hibernate inheritance strategies with annotations

 Hibernate supports 3 types of inheritance.

1. Table Per Class Hierarchy (Single Table Strategy)

In table per class hierarchy, Hibernate will store base class data and all its derived class data into a single table in the database.

Here we have to use @DiscriminatorColumn to base class .
And we have to use @Inheritance(strategy = InheritanceType.SINGLE_TABLE) to base class. 
And @DiscriminatorValue to derived classes

✅ When to Use:

  • When performance is more important than normalization.

  • When all subclasses share most fields.

📘 Inheritance Class Diagram

+----------------+ | Person | <-- @Entity (Superclass) +----------------+ | id : Long | | name : String | +----------------+ ▲ /-------+--------\ / \ +----------------+ +----------------+ | Employee | | Customer | <-- @Entity (Subclasses) +----------------+ +----------------+ | department | | address | +----------------+ +----------------+
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "person_type", discriminatorType = DiscriminatorType.STRING)
public class Person {
    @Id
    private Long id;
    private String name;
}

@Entity
@DiscriminatorValue("EMP")
public class Employee extends Person {
    private String department;
}

@Entity
@DiscriminatorValue("CUST")
public class Customer extends Person {
    private String address;
}

 2. Table Per Subclass (Joined Table Strategy)

In this strategy, the base class and derived class data will be stored in different tables in the database, means each class has its own table.

⚠️ We have use @Inheritance(strategy = InheritanceType.JOINED)

✅ When to Use:

  • When normalized schema is required.

  • When you want to reduce null columns in tables.

📄 Example with Annotations:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Person {
    @Id
    private Long id;
    private String name;
}

@Entity
public class Employee extends Person {
    private String department;
}

@Entity
public class Customer extends Person {
    private String address;
}

 3. Table Per Concrete Class (Table Per Class Strategy)

In this strategy, Hibernate stores the base class data and derived class data into the derived class tables only.
No separate table is required for the base class. And we have to use @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

✅ When to Use:

  • When no polymorphic queries are needed.

  • When subclasses don’t relate closely.

📄 Example with Annotations:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Person {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private String company;
}

@Entity
public class Employee extends Person {
    private double salary;
    private String department;
}

@Entity
public class Customer extends Person {
    private String address;
}

🔍 Summary Table

StrategyOne TableNormalizedSpeedDiscriminatorTable for Base
Single Table
Table per Subclass (Joined)⚠️ Slower❌ (optional)
Table per Concrete Class⚠️ Redundant

No comments:

Post a Comment