Inheritance Mapping Strategies in Hibernate Explained

Answered using GPT-4o-mini by Java Q&A Hub

Question:

What are the inheritance mapping strategies in Hibernate?

Answer:

In Hibernate, inheritance mapping strategies are used to map an object-oriented class hierarchy to a relational database schema. Hibernate provides several strategies to handle inheritance, allowing developers to choose the one that best fits their application's requirements. The main inheritance mapping strategies in Hibernate are:

  1. Single Table Inheritance:

    • In this strategy, a single table is used to store all classes in the inheritance hierarchy. A discriminator column is added to differentiate between the different subclasses.
    • Advantages:
      • Simplicity and performance, as only one table is queried.
      • No need for joins.
    • Disadvantages:
      • Can lead to a sparse table if subclasses have many unique fields.
    • Example:
      @Entity
      @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
      @DiscriminatorColumn(name = "TYPE")
      public abstract class Animal { ... }
      
      @Entity
      @DiscriminatorValue("DOG")
      public class Dog extends Animal { ... }
      
      @Entity
      @DiscriminatorValue("CAT")
      public class Cat extends Animal { ... }
      
  2. Table Per Class Inheritance:

    • Each class in the hierarchy is mapped to its own table. The table for a subclass contains all the fields of the subclass and the fields of its superclass.
    • Advantages:
      • Each table is specific to a class, which can lead to a more normalized database design.
    • Disadvantages:
      • Queries that involve the superclass may require joins, which can impact performance.
    • Example:
      @Entity
      @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
      public abstract class Animal { ... }
      
      @Entity
      public class Dog extends Animal { ... }
      
      @Entity
      public class Cat extends Animal { ... }
      
  3. Joined Table Inheritance:

    • In this strategy, each class in the hierarchy is mapped to its own table, but the subclass tables only contain the fields specific to the subclass. The superclass table contains the common fields, and joins are used to retrieve the complete object.
    • Advantages:
      • More normalized than single table inheritance, reducing redundancy.
      • Allows for a clear separation of common and specific fields.
    • Disadvantages:
      • Requires joins to retrieve data, which can affect performance.
    • Example:
      @Entity
      @Inheritance(strategy = InheritanceType.JOINED)
      public abstract class Animal { ... }
      
      @Entity
      public class Dog extends Animal { ... }
      
      @Entity
      public class Cat extends Animal { ... }
      
  4. Mapped Superclass:

    • This is not a true inheritance strategy but allows you to define a superclass that is not an entity itself but can be inherited by other entities. The fields of the mapped superclass are included in the subclass tables.
    • Advantages:
      • Useful for code reuse without creating a separate table for the superclass.
    • Disadvantages:
      • Cannot be queried directly as it does not have its own table.
    • Example:
      @MappedSuperclass
      public abstract class BaseEntity {
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          private Long id;
          // other common fields
      }
      
      @Entity
      public class Dog extends BaseEntity { ... }
      
      @Entity
      public class Cat extends BaseEntity { ... }
      

Each of these strategies has its own use cases, and the choice of which to use depends on factors such as the complexity of the class hierarchy, performance considerations, and the specific requirements of the application.