What are inheritance mapping strategies in JPA

Upasana | May 05, 2019 | 2 min read | 499 views


JPA defines three inheritance strategies namely, SINGLE_TABLE, TABLE_PER_CLASS and JOINED.

Single table inheritance is default, and table per class is optional so all JPA vendors may not support it. JPA also defines mapped super class concept defined through the @MappedSuperClass annotation. A Mapped Super Class is not a persistent class, but allows a common persistable mapping to be defined for its subclasses.

Single Table Inheritance

In this inheritance, a single table is used to store all the instances of the entire inheritance hierarchy. The Table will have a column for every attribute of every class in the hierarchy. Discriminator columns identifies which class a particular row belongs.

SINGLE_TABLE Inheritance
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Product {
    @Id
    private long id;
    private String name;

}

@Entity
public class Book extends Product {
    private String isbn;
}

@Entity
public class DigitalProduct extends Product {
    private String url;
}

with this mapping strategy, only a single table will be created for both concrete classes (Book and DigitalProduct). Hibernate will create a discriminator column named DTYPE to differentiate each concrete type. The value of this column will be name of the entity (Book or DigitalProduct).

Table Per Class Inheritance

A table is defined for each concrete class in the inheritance hierarchy to store all the attribute of that class and all its super classes.

Joined Table

This inheritance replicates the object model into data model. A table is created for each class in the hierarchy to store only the local attributes of that class.

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Product {
    @Id
    private long id;
    private String name;

}

@Entity
public class Book extends Product {
    private String isbn;
}

The above mapping will create two tables, one for super class Product and another for entity Book. Both the tables will have Product id identifier column. The primary key of table Book will have foreign key relationship with primary key of Product table.


We want to extract common behavior in a super class in JPA entities but without having a table for that super class. How would you achieve that?

If we create a normal class as the super class, then as per JPA specifications, the fields for that class are not persisted in the database tables. We need to create a super class extracting the common fields and then annotate that class with @MappedSuperClass in order to persist the fields of that super class in subclass tables. A mapped super class has no separate table defined for it.

MappedSuperClass example
@MappedSuperclass
public class Person {

    @Id
    private long id;
    private String name;

}

@Entity
public class Employee extends Person {
    private String company;

}

The above configuration will result in a single table for employee with 3 columns (id, name and company), 1 from Employee class itself and 2 are inherited from Person class. Person class will never have its own table in this case.


Top articles in this category:
  1. Prevent Lost Updates in Database Transaction using Spring Hibernate
  2. N+1 problem in Hibernate & Spring Data JPA
  3. What are different Bean Scopes in Spring?
  4. What is difference between Component, Repository, Service, Controller & RestController
  5. Basic Auth Security in Spring Boot 2
  6. Table backed global counter in spring hibernate
  7. Redis rate limiter in Spring Boot

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.