Hibernate provides several ways to map inheritance hierarchies in a relational database. The most common inheritance mapping strategies are:
Table per class hierarchy: Each class in the hierarchy has its own table in the database, with a column for the class type to differentiate between the different classes.
Table per subclass: Each subclass in the hierarchy has its own table in the database, with a foreign key to the superclass table.
Table per concrete class: Each concrete class in the hierarchy has its own table in the database, with no foreign key to the superclass table
InheritanceType.JOINED. This tells Hibernate to use the table per class hierarchy strategy. With this strategy, Hibernate will create a single table for the entire hierarchy, with additional columns for the properties of the subclasses. The table will have a discriminator column that is used to determine which class a particular row corresponds to.
Here's an example of the table that would be created for this hierarchy:
EMPLOYEE +----+---------+----------+-------+ | ID | NAME | TYPE | SALARY| +----+---------+----------+-------+ | 1 | Vikash | FullTime | 100000| +----+---------+----------+-------+ | 2 | Sakshi | PartTime | 20 | +----+---------+----------+-------+
Here, the TYPE column is the discriminator column that tells us if it's a FullTimeEmployee or PartTimeEmployee.
With this strategy, it's easy to retrieve all employees, but it could lead to a large amount of null values in the table, as subclasses properties are null for the superclass objects.
This strategy is best suited for small and simple inheritance hierarchies with few classes, where all classes have a small number of properties and the performance overhead of joins is not a significant concern.
Here is an example of how to map an inheritance hierarchy using the table per class hierarchy strategy:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee {
@Id
@GeneratedValue
private int id;
private String name;
// getters and setters
}
@Entity
public class FullTimeEmployee extends Employee {
private double salary;
// getters and setters
}
@Entity
public class PartTimeEmployee extends Employee {
private double hourlyRate;
// getters and setters
}
In this example, we have a base class Employee with two subclasses FullTimeEmployee and PartTimeEmployee. We use the @Inheritance annotation on the Employee class to specify the strategy.
There are other strategies like table per subclass, table per concrete class with their own advantages and disadvantages, You can choose the one that best suits your use case.
Table per subclass is another strategy provided by Hibernate to map inheritance hierarchies in a relational database. In this strategy, each subclass in the hierarchy has its own table in the database, with a foreign key to the superclass table.
Here is an example of how to map an inheritance hierarchy using the table per subclass strategy:
@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class Employee { @Id @GeneratedValue private int id; private String name; // getters and setters } @Entity public class FullTimeEmployee extends Employee { private double salary; // getters and setters } @Entity public class PartTimeEmployee extends Employee { private double hourlyRate; // getters and setters }
In this example, we have a base class Employee with two subclasses FullTimeEmployee and PartTimeEmployee. We use the @Inheritance annotation on the Employee class to specify the strategy as InheritanceType.TABLE_PER_CLASS.
With this strategy, Hibernate will create a separate table for each subclass, with a foreign key to the superclass table. Here's an example of the tables that would be created for this hierarchy:
EMPLOYEE +----+---------+ | ID | NAME | +----+---------+ | 1 | Vikash | +----+---------+ | 2 | Sakshi | +----+---------+
FULLTIME_EMPLOYEE +----+-------------+ | ID | SALARY | +----+-------------+ | 1 | 100000 | +----+-------------+
PARTTIME_EMPLOYEE +----+-------------+ | ID | HOURLY_RATE | +----+-------------+ | 2 | 20 | +----+-------------+
Here, the EMPLOYEE table contains the common properties of all classes, and the FULLTIME_EMPLOYEE and PARTTIME_EMPLOYEE tables contain the specific properties of the subclasses.
Table per concrete class is another strategy provided by Hibernate to map inheritance hierarchies in a relational database. In this strategy, each concrete class in the hierarchy has its own table in the database, with no foreign key to the superclass table.
Here is an example of how to map an inheritance hierarchy using the table per concrete class strategy:
@MappedSuperclass public abstract class Employee { @Id @GeneratedValue private int id; private String name; // getters and setters }
@Entity public class FullTimeEmployee extends Employee { private double salary; // getters and setters }
@Entity public class PartTimeEmployee extends Employee { private double hourlyRate; // getters and setters }
In this example, we have a base class Employee with two subclasses FullTimeEmployee and PartTimeEmployee. We use the @MappedSuperclass annotation on the Employee class to indicate that it's not an entity, and it's only used as a superclass for other entities
With this strategy, Hibernate will create a separate table for each concrete class, with no foreign key to the superclass table. Here's an example of the tables that would be created for this hierarchy:
FULLTIME_EMPLOYEE +----+---------+-------------+ | ID | NAME | SALARY | +----+---------+-------------+ | 1 | Vikash | 100000 | +----+---------+-------------+
PARTTIME_EMPLOYEE +----+---------+-------------+ | ID | NAME | HOURLY_RATE | +----+---------+-------------+ | 2 | Sakshi | 20 | +----+---------+-------------+