Home Inheritance Mapping
Post
Cancel

Inheritance Mapping

Inheritance Mapping:

Certainly! Inheritance is a fundamental concept in object-oriented programming, and it’s also an important aspect of the Java Persistence API (JPA). In this blog, we’ll explore the different types of inheritance in JPA, and next blog we will discuss how these can be implemented.

In JPA, there are three types of inheritance: single table, joined, and table per class hierarchy. Each type of inheritance has its own advantages and disadvantages, and the choice of which one to use depends on the specific requirements of the application.

Table per class hierarchy (Single Table Strategy):

Single table inheritance is a type of inheritance in which all the fields from the superclass and the subclass are stored in a single database table. The subclass entities share the same primary key with the superclass entity. This type of inheritance is used when there is a small number of subclasses with a lot of common fields.

The advantage of using single table inheritance is that it’s very efficient in terms of database performance, since there is only one table to query. However, it can lead to a lot of null values in the table for fields that don’t apply to certain subclasses.

Joined Inheritance:

Joined inheritance is a type of inheritance in which the fields from the superclass and the subclass are stored in separate database tables. The subclass tables have a foreign key relationship with the superclass table, and the superclass table contains only the fields that are common to all of the subclasses.

The advantage of using joined inheritance is that it reduces the amount of null values in the table, since each subclass has its own table with only the fields that apply to it. However, it can be less efficient in terms of database performance, since queries often involve multiple tables.

Table Per Class Inheritance:

In this strategy, each concrete class in the inheritance hierarchy is mapped to its own table in the database. As each class has its own table, queries on each table can be faster as they only deal with a limited amount of data. But table per class inheritance, each table has a copy of the common fields. This can lead to redundancy and increase the size of the database.

Conclusion

In conclusion, inheritance is an important aspect of object-oriented programming, and it’s also an important concept in the Java Persistence API. JPA provides three strategies for implementing inheritance: single table, joined, and table per class hierarchy. Each strategy has its own advantages and disadvantages

This post is licensed under CC BY 4.0 by the author.