In JPA (Java Persistence API), inheritance is a powerful feature that allows the entities to inherit the properties from other entities. This is particularly can be useful for representing the real-world inheritance relationships in the database schema. In this article, we will guide the different strategies for inheritance in JPA.
JPA Inheritance
Inheritance is the fundamental concept in object-oriented programming that allows the class to inherit properties and methods from another class. JPA supports the inheritance in the entity classes. It allows the developers to model the real-world inheritance hierarchies in the relational database. JPA provides the three main strategies for mapping inheritance relationships to the database tables:
- Single Table Strategy
- Joined Table Strategy
- Table Per Class Strategy
JPA Inheritance Strategies
.png)
Single Table Strategy
In the Single Table strategy, all the classes in the inheritance hierarchy are mapped to the single database table. The discriminator column can be used to identify the type of the each row. This strategy can efficient in the terms of the query performance but can lead to the sparse table if the hierarchy is large.
Example:
import javax.persistence.*;
@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;
// Getters and setters
}
@Entity
@DiscriminatorValue("FULL_TIME")
public class FullTimeEmployee extends Employee {
private double salary;
// Getters and setters
}
@Entity
@DiscriminatorValue("PART_TIME")
public class PartTimeEmployee extends Employee {
private double hourlyRate;
// Getters and setters
}Joined Table Strategy
In the Joined Table strategy, each class in the hierarchy is mapped to its own table. The base class table contains the common attributes and the each subclass table contains the attributes specific to that subclass. This strategy can be normalizes the database schema but can result in the more complex SQL joins.
Example:
import javax.persistence.*;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long 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
}Table Per Class Strategy
In the Table Per Class strategy, each concrete class in the hierarchy can mapped to its own table. This strategy can provides the best isolation between the tables but can lead to the data redundancy and does not support polymorphic queries well.
Example:
import javax.persistence.*;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long 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
}Conclusion
JPA inheritance strategies provides the flexibility in the designing the database schema to the match the applications object model. The choice of the strategy depends on the specific use case and performance requirements of the application.
- Single Table Strategy can efficient but may lead to the sparse tables.
- Joined Table Strategy can normalizes the schema but can introduce the complexity in joins.
- Table Per class Strategy can isolates the table but can lead to the redundancy.
By the understanding these strategies and their implications, developers can make informed the decisions when modeling the inheritance relationships in their JPA applications.