Home » JPA Inheritance Overview

JPA Inheritance Overview

by Online Tutorials Library

JPA Inheritence Overview

Inheritence is a key feature of object-oriented programming language in which a child class can acquire the properties of its parent class. This feature enhances reusability of the code.

The relational database doesn’t support the mechanism of inheritance. So, Java Persistence API (JPA) is used to map the key features of inheritance in relational database model.

JPA Inheritence Annotations

Following are the most frequently used JPA inheritance annotations: –

  • @Inheritence – This annotation is applied on the root entity class to define the inheritance strategy. If no strategy type is defined with this annotation then it follows single table strategy.
  • @MappedSuperclass – This annotation is applied to the classes that are inherited by their subclasses. The mapped superclass doesn’t contain any separate table.
  • @DiscriminatorColumn – The discriminator attribute differentiates one entity from another. Thus, this annotation is used to provide the name of discriminator column. It is required to specify this annotation on the root entity class only.
  • @DiscriminatorValue – This annotation is used to specify the type of value that represents the particular entity. It is required to specify this annotation on the sub-entity classes.

Note – If we didn’t pass the name of discriminator column and its value then JPA consider it by default. It considers DTYPE as discriminator column name and the name of the entity as discriminator value.

JPA Inheritence Strategies

JPA provides three strategies through which we can easily persist inheritance in database.

You may also like