Table of contents
1.
🙋Introduction
2.
What is Inheritance Mapping?
3.
🤓Four Strategies for Inheritance Mapping
4.
Mapped Superclass Strategy💡
5.
Single Table Strategy💡
6.
Joined Table Strategy💡
7.
Table Per Class Strategy💡
8.
Frequently Asked Questions❓
8.1.
What is inheritance mapping?
8.2.
Can we use Mapped Superclass and Entity annotation together in a class?
8.3.
What is the default value for the Inheritance annotation?
8.4.
What are the two predefined values for the DiscriminatorValue annotation?
9.
Conclusion🔚
Last Updated: Mar 27, 2024
Medium

What is Inheritance Mapping?

Author Adya Tiwari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

🙋Introduction

Before diving deep into the topic, lets discuss inheritance first. Inheritance is a one of the four pillers in OOPs.  In simple terms, we can understand it as the reusability of existing classes.  You can read about inheritance in detail in this article

What is Inheritance Mapping?

Suppose we have an inheritance in our Domain Model(will see in the examples), which is an easy way to represent complicated Object-Oriented Logic.  

We do not have a specific way to support class hierarchies to map these models to a Relational Database such as SQL.  But 

Hibernate provides us with four ways to support Inheritance Mapping, which we will discuss throughout the article. 

🤓Four Strategies for Inheritance Mapping

Inheritance Mapping is the mapping of class hierarchies in databases.  The four strategies through which we can achieve this are:


 

  • Mapped Superclass
  • Single Table
  • Joined Table
  • Table per Class

Let us discuss all these in detail.

Mapped Superclass Strategy💡

This is the simplest way to approach inheritance mapping.  In this technique, we have a class present only for mapping.  It is not an entity, meaning there will be no table for the Mapped Superclass. The mapped superclass does not have the @entity annotation but the @MappedSuperclass annotation.  Let us understand it through an example:

 

Domain Model for the example

@MappedSuperclass
public class Gadget {
    @Id
    private String category;
    // body
}


@Entity
public class Cellphone extends Gadget {
    private String model;
    private String ctype;
    // body
}

 

Now in this example, we have two classes: first, Gadget and second, Cellphone.  The Gadget class is a MappedSuperclass, and the second Cellphone class inherits the Gadget class.  We only have tables for entities.  The mapped superclass is never an entity.

Looking at the database side now, we will have one table for the Cellphone class having three attributes:  category, model, and ctype

Now because we do not have a table for the Gadget class, we cannot search for a Gadget with attributes using an Entity Manager.  We will only be able to perform queries on the Cellphone entity.

Single Table Strategy💡

In this strategy, we have all the classes in a hierarchy in a single table, thus the name Single Table Strategy.  It means that all the hierarchical classes will have a single table in the database.  We have different annotations we use in this strategy:

@Inheritance: This annotation specifies the type of strategy we are using.

@DiscriminatorColumn: Because all the entities are present in the same table, we need some way to distinguish them.  We have the DiscriminatorColumn annotation to specify the type of entity.

@DiscriminatorValue: This represents the value for each entity for the DiscriminatorColumn.  Every subclass entity has a unique value.

Let us understand these terms through an example.

 

Domain Model for the example

 

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="Gadget_Type")
public class Gadget {
    @Id
    private String category;
    // body
}


@Entity
@DiscriminatorValue("1")
public class Cellphone extends Gadget {
    private String model;
    private String ctype;
    // body
}


@Entity
@DiscriminatorValue("2")
public class VaccumCleaner extends Gadget {
    private String company;
    private String watt;
    // body
}

 

In this example, we have one superclass Gadget and two subclasses Cellphone and VaccumCleaner.  We specify the inheritance strategy in the superclass as SINGLE_TABLE.  We name the discriminator column as Gadget_Type in the super class.  This column will have as many unique values as the number of subclasses for the Gadget class. 

The table for this example will have 6 columns: Gadget_Type, category, model, ctype, company, and watt.

The subclass Cellphone has a Discriminator value 1, and VaccumCleaner has a value 2.  These are the values that the Gadget_Type column will store depending on the type of entry.  The entry will be either for the Cellphone or VaccumCleaner class.  In this technique, we can query all three tables.

Joined Table Strategy💡

The technique in which every entity has its table.  Every table will have columns corresponding to the number of member variables or attributes present in the entity(class).  We have an @Id attribute which will be present in every table.  This id attribute comes into play when we join two tables.

The value for the Inheritance annotation for this strategy is Joined.  Let us see this through an example:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Gadget {
    @Id
    private String category;
    // body
}


@Entity
public class Cellphone extends Gadget {
    @Id
    private String category;
    private String model;
    private String ctype;
    // body
}

 

We will have 2 tables for this example. Gadget and Cellphone will have separate tables.  The id attribute- category will be a column in both tables.  The primary key of the subclasses will have a foreign key constraint to the primary key of the parent classes. 

Table Per Class Strategy💡

The last strategy for inheritance mapping is the table per class strategy.  In this, we have a table per entity.  The table for each entity has its attributes and the attributes of its parent class.  Unlike the mapped superclass strategy, this technique has a table for the superclass.  Let us see an example:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Gadget {
    @Id
    private String category;
    // body
}


@Entity
public class Cellphone extends Gadget {
    private String model;
    private String ctype;
    // body
}

 

In this example, we will have a table for both Gadget and Cellphone classes.  We will also be able to perform queries on both entities.  

These were the strategies we have for inheritance mapping in Hibernate.

Frequently Asked Questions❓

What is inheritance mapping?

Inheritance Mapping is the mapping of class hierarchies in databases.  There are four ways to achieve inheritance mapping: Mapped Superclass, Single Table, Joined Table, and Table per Class.

Can we use Mapped Superclass and Entity annotation together in a class?

No, we can not have a Mapped Superclass and Entity annotation together for a class.

What is the default value for the Inheritance annotation?

In Hibernate, the default value for the inheritance annotation is SINGLE_TABLE.

What are the two predefined values for the DiscriminatorValue annotation?

The DiscriminatorValue annotation can have two predefined values: null or not null.  The row with no discriminator value maps to the class which has null in its DiscriminatorValue annotation.  The row with a value matching none of the discriminator values will map to the one that has not null as the DiscriminatorValue.

Conclusion🔚

In this article, we brushed up on the idea of inheritance.  We learned what inheritance mapping is in hibernate and how we achieve inheritance mapping.  We saw the four strategies to map hierarchies in the class to the database.

Don't stop yourself here.  Also, practice data structures and algorithmsinterview questionsDBMScomputer networks, and operating systems to crack the interviews of big tech giants.  Explore other fields like machine learningdeep learningcomputer vision, and big data.  Also, check out Interview Experiences for different companies.

Happy learning!

 

Live masterclass