📜  https: www.baeldung.com hibernate-inheritance - Java (1)

📅  最后修改于: 2023-12-03 15:31:20.420000             🧑  作者: Mango

Hibernate Inheritance

Hibernate Inheritance is a powerful feature that allows developers to create hierarchies of related classes with different levels of abstraction and specialization that can be mapped to a single table or a set of tables in a database.

In this article, we're going to explore Hibernate Inheritance in Java and its various mapping strategies.

Table Per Class Hierarchy

The Table Per Class Hierarchy mapping strategy is the simplest and the most common inheritance mapping strategy used in Hibernate. In this strategy, all the subclasses/entities are mapped to a single database table, and a discriminator column is used to identify the type of the record.

Here's an example of how to implement the Table Per Class Hierarchy strategy in Hibernate:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "employee_type", discriminatorType = DiscriminatorType.STRING)
public abstract class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;
    
    // getters and setters
}

@Entity
@DiscriminatorValue(value = "FullTime")
public class FullTimeEmployee extends Employee {
    private double salary;
    
    // getters and setters
}

@Entity
@DiscriminatorValue(value = "PartTime")
public class PartTimeEmployee extends Employee {
    private double wage;
    
    // getters and setters
}
Table Per Subclass

The Table Per Subclass inheritance mapping strategy is another commonly used strategy in Hibernate. In this strategy, each subclass is mapped to its own table, and a foreign key is used to map the subclass to the superclass.

Here's an example of how to implement the Table Per Subclass strategy in Hibernate:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;
    
    // getters and setters
}

@Entity
public class FullTimeEmployee extends Employee {
    private double salary;
    
    // getters and setters
}

@Entity
public class PartTimeEmployee extends Employee {
    private double wage;
    
    // getters and setters
}
Table Per Concrete Class

The Table Per Concrete Class inheritance mapping strategy is the least common strategy used in Hibernate. In this strategy, each concrete class is mapped to its own table without any relationship between them.

Here's an example of how to implement the Table Per Concrete Class strategy in Hibernate:

@Entity
public class FullTimeEmployee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;
    private double salary;
    
    // getters and setters
}

@Entity
public class PartTimeEmployee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;
    private double wage;
    
    // getters and setters
}
Conclusion

Hibernate Inheritance is a powerful feature that allows developers to create hierarchies of related classes with different levels of abstraction and specialization that can be mapped to a single table or a set of tables in a database. In this article, we've explored the three most common inheritance mapping strategies used in Hibernate, including Table Per Class Hierarchy, Table Per Subclass, and Table Per Concrete Class.