The @Inheritance annotation in JPA is used to specify the inheritance relation between two entities. It is used to define how the data of the entities in the hierarchy should be stored in the database. The @Inheritance annotation provides us with benefits to reduce code complexity by creating a base class and inheriting other classes from the base class. The @Inheritance annotation is applied to the root entity class to define the inheritance strategy. There are different types of strategies available for Inheritance annotation which are as follows:
- Single Table Inheritance Strategy.
- Joined Inheritance Strategy.
- Table Per Class Inheritance Strategy.
If no strategy is defined while using @inheritance annotation the default Single Table Inheritance Strategy is applied.
1. Single Table Inheritance Strategy
The Single Table Inheritance strategy states that all the entities in this hierarchy are mapped to a single table in the database. Separate columns are used to differentiate between multiple entities. This strategy is suitable when entities in the hierarchy share most of their attributes and have a small number of different fields.
Example for Single Table Inheritance Strategy:
Java
// on the below line creating an entity for Student @Entity @Inheritance (strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn (name = "entity_type" , discriminatorType = DiscriminatorType.STRING) public class Student { // on the below line creating a field for id which we // are annotating with @Id. @Id private int id; // on the below line creating a field for the student // name. private String studentName; } // on the below line creating a separate entity for boys in // the group of students. @Entity // on the below line specifying the discriminator value as // boy. @DiscriminatorValue ( "BOY" ) // on the below line creating a class for boy and extending // it with Student public class Boy extends Student { // Define Boys-specific properties in this class. } @Entity // on the below line specifying the discriminator value as // girl. @DiscriminatorValue ( "GIRL" ) // on the below line creating a class for girl and extending // it with Student public class Girl extends Student { // Define Girls-specific properties in this class. } |
Code Explanation:
In the above example, we are creating a Base class for Students which will serve for both boys as well as girls. Inside this class we are specifying the common properties such as id, name. We are adding an inheritance annotation for the Student base class as InheritanceType.SINGLE_TABLE which indicates the Single Table inheritance strategy. The discriminator column entity_type is used to differentiate between entities and it is specified with @DiscriminatorColumn annotation. The discriminator values for each entity are set using @DiscriminatorValue annotation.
2. Joined Inheritance Strategy
The Joined Inheritance Strategy generates a separate table for each entity class. The attribute for each table is joined with the primary key. It removes the possibility of duplicity. Each table that is being created contains specific columns and a join operation is used to retrieve data from multiple tables when querying the entire hierarchy. We can use the strategy when the entities we are creating have differences in their properties and relationships.
Example forJoined Inheritance Strategy:
Java
// on the below line creating an entity for Student @Entity // on the below line we are specifying the inheritance // strategy as joined. @Inheritance (strategy = InheritanceType.JOINED) public class Student { // on the below line creating a field for id which we // are anotating it with @Id. @Id private int id; // on the below line creating a field for student name. private String studentName; } // on the below line creating a seperate entity for boys in // the group of students. @Entity // on the below line specifying the table name for boys. @Table (name = "boys" ) // on the below line creating a class for boy and extending // it with Student public class Boy extends Student { // Define Boys-specific properties in this class. } @Entity // on the below line specifying the table name for girl. @Table (name = "girls" ) // on the below line creating a class for girl and extending // it with Student public class Girl extends Student { // Define Girls-specific properties in this class. } |
Code Explanation:
In the above example, we are creating three entities for Student, Boys, and Girls. Each entity that is being created has its own table in the database. For boys entities a new table will be created as boys. Similarly, for Girl entities, a new table will be created for girls. The @Inheritance annotation is set to InheritanceType.JOINED which indicates that the Joined Inheritance Strategy. We are creating a base class as Student which is a base entity and contains some common fields for both boys as well as girls such as name and id.
3. Table Per Class Inheritance Strategy
In the table per class inheritance strategy, a separate table is generated for each subclass. Each entity is mapped to its own table in the database, unlike joined strategy no separate table is generated for the parent entity class in the table per class strategy. This strategy provides us with the highest level of flexibility but this will result in redundant columns and more complex database schema.
Example for Table Per Class Inheritance Strategy:
Java
// on the below line creating an entity for Student @Entity // on the below line we are specifying the inheritance // strategy as tabler per class. @Inheritance (strategy = InheritanceType.TABLE_PER_CLASS) public class Student { // on the below line creating a field for id which we // are annotating with @Id. @Id private int id; // on the below line creating a field for the student // name. private String studentName; } // on the below line creating a separate entity for boys in // the group of students. @Entity // on the below line creating a class for boy and extending // it with Student public class Boy extends Student { // Define Boys-specific properties in this class. } // on the below line creating an entity for girl in the // group of students. @Entity // on the below line creating a class for girl and extending // it with Student public class Girl extends Student { // Define Girls-specific properties in this class. } |
Code Explanation:
In the above example, each entity that we are creating from the Student entity such as Boy and Girl entity has its own table present in the database. The @Inheritance annotation is set to InheritanceType.TABLE_PER_CLASS indicates the Table Per class inheritance strategy. The Base class student contains some common fields in it such as an id for the student and a field for the Student name.