When to Use @DiscriminatorValue Annotation in Hibernate?
Last Updated :
21 Jun, 2023
The @DiscriminatorColumn annotation in JPA is used to differentiate one entity from another entity class from an inheritance hierarchy. This annotation is used to provide the name of the discriminator column. It is required to specify this annotation on the root entity class only. The @DiscriminatorColumn annotation in JPA defines the discriminator column for entities that are part of the inheritance hierarchy. It specifies the discriminator value which is used to differentiate between different types of entities in the hierarchy.
Example for @DiscriminatorColumn Annotation
Example 1:
Java
@Entity
@Inheritance (strategy = InheritanceType.SINGLE_TABLE)
public class Student {
@Id
private int id;
private String studentName;
}
@Entity
@DiscriminatorValue ( "boy" )
public class Boy extends Student {
}
@Entity
@DiscriminatorValue ( "girl" )
public class Girl extends Student {
}
|
Code Explanation:
In the above example, we are creating a base class for Students that has different fields such as id and student name. Then we are creating two separate classes for boys and girls which are inherited from the student class. We are adding annotation for @Inheritance on our base class and specifying the strategy as a Single table. The Discriminator column annotation is used to define the discriminator column as entity_type, which will hold the value that differentiates between the types of entities in the hierarchy. The name attribute in it will specify the name of the discriminator column in the database and the discriminatorType attribute specifies the data type of the discriminator column. We are using @DiscriminatorValue annotation for each entity (Boy and Girl) to specify the discriminator value for that specific entity. In our case, we are specifying the discriminator value for boys entity as boy and for girls entity as girl.
Example 2:
Java
@Entity
@Inheritance (strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn (name = "entity_type" ,
discriminatorType
= DiscriminatorType.STRING)
public class Animal {
@Id
private int id;
private String animalName;
}
@Entity
@DiscriminatorValue ( "dog" )
public class Dog extends Animal {
}
@Entity
@DiscriminatorValue ( "cow" )
public class Cow extends Animal {
}
|
Code Explanation:
In the above example, we are creating a base class for Animal which has different fields such as id and animal name. Then we are creating two separate entities for cows and dogs which are inherited from animal class. We are adding annotation for @Inheritance on our base class and specifying the strategy as Single Table. The Discriminator column annotation is used to bring the discriminator column as entity_type, which will hold the value that differentiates between the types of entities in the hierarchy. The name attribute in it will specify the name of the discriminator column in the database and the discriminatorType attribute specifies the data type of the discriminator column. We are using @DiscriminatorValue annotation for each entity (Dog and Cow) to specify the discriminator value for that specific entity. In our case, we are specifying the discriminator value for the cows entity as cow and for the dogs entity as dog.
Share your thoughts in the comments
Please Login to comment...