Open In App

When to Use @DiscriminatorValue Annotation in Hibernate?

Last Updated : 21 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// on the below line creating an entity for Student
@Entity
// on the below line we are specifying the inheritance
// strategy as single table.
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Student {
    // on the below line creating a field for id which we
    // are annotating 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 separate entity for boys in
// the group of students.
@Entity
// on the below line adding annotation for discriminator
// value and passing is 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.
}
// on the below line creating an entity for girl in the
// group of students.
@Entity
// on the below line adding annotation for discriminator
// value and passing is 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 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




// on the below line creating an entity for Animal
@Entity
// on the below line we are specifying the inheritance
// strategy as single table.
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "entity_type",
                     discriminatorType
                     = DiscriminatorType.STRING)
public class Animal {
    // on the below line creating a field for id which we
    // are annotating it with @Id.
    @Id 
      private int id;
    // on the below line creating a field for animal name.
    private String animalName;
}
// on the below line creating a separate entity for Dog in
// the group of animals.
@Entity
// on the below line adding annotation for discriminator
// value and passing is as dog
@DiscriminatorValue("dog")
// on the below line creating a class for boy and extending
// it with Student
public class Dog extends Animal {
    // Define Dog-specific properties in this class.
}
// on the below line creating an entity for Cow in the group
// of students.
@Entity
// on the below line adding annotation for discriminator
// value and passing it as cow
@DiscriminatorValue("cow")
// on the below line creating a class for cow and extending
// it with Animal
public class Cow extends Animal {
    // Define Cows-specific properties in this class.
}


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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads