Entity Inheritance


The course is part of this learning path

Start course

This course covers the Java Persistence API, or JPA, which is designed to persist data between Java objects and relational databases.

Learning Objectives

  • Learn the fundamentals of the Java Persistence API
  • Learn about entities, entity inheritance, and JPA queries

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java Enterprise Edition.


Basic knowledge of Java programming.


Hello, dear friends. In this video, we will examine the entity inheritance, let's begin. Entities support class inheritance, polymorphic associations, and polymorphic queries. Entity classes can extend non entity classes, and non entity classes can extend entity classes. Entity classes can be both abstract and concrete. There are many types of inheritance in JPA. Let's examine them one by one. First one is abstract entities. An abstract class may be declared an entity by decorating the class with entity. Abstract entities are like concrete entities but cannot be instantiated. Abstract entities can be queried just like concrete entities. If an abstract entity is the target of a query, the query operates on all the concrete subclasses of the abstract entity. As an example, if we define an abstract class like this, after that we can extend each class from this abstract. If we were to use these entities, the database tables would look like this. Look, two tables are visible and each table contains abstract class fields.

The other one is mapped super class. Entities may inherit from superclasses that contain persistent state and mapping information but are not entities, that is, the super class is not decorated with the entity annotation and is not mapped as an entity by the Java persistence provider. These superclasses are most often used when you have state and mapping information, comment to multiple entity classes. Mapped superclasses are specified by decorating the class with the annotation, JavaX Persistence mapped super class. 

For example, this class has a mapped super class annotation. If we add another two entities with extends mapped super class, the database will look like this. We cannot see any base table in the database. Mapped superclasses cannot be queried and can't be used in entity manager or query operations. You must use entity subclasses of the mapped superclass in entity manager or query operations. Mapped superclasses can be targets of entity relationships. Mapped superclasses can be abstract or concrete. Mapped superclasses do not have any corresponding tables in the underlying data source. Entities that inherit from the mapped super class define the table mappings.

Entities may have non-entity superclasses, and these superclasses can either be abstract or concrete. The state of non-entity superclasses is non persistent in any state inherited from the non-entity superclass by an entity class is non-persistent. Non-entity superclasses may not be used in entity manager or query operations. Any mapping or relationship annotations in non-entity super classes are ignored. Let's look at the entity inheritance mapping strategies. You can configure how the Java persistence provider maps inherited entities to the underlying data source by decorating the root class of the hierarchy with the annotation JavaX persistence inheritance. 

Three mapping strategies are used to map the entity data to the underlying database. A single table per class hierarchy, a table per concrete entity class, and a joint strategy whereby fields or properties that are specific to a subclass are mapped to a different table than the fields or properties that are common to the parent class. The strategy is configured by setting the strategy element of inheritance to one of the options defined in the Java persistence inheritance type enumerated type. The default strategy, inheritance type.single_table is used if the inheritance annotation is not specified on the root class of the entity hierarchy. Now, let's examine them. With a single table per class hierarchy strategy, which corresponds to the default inheritance type SINGLE_TABLE, all classes in the hierarchy are mapped to a single table in the database.

This table has a discriminator column containing a value that identifies the subclass to which the instance represented by the row belongs. As you can see in this table, the discriminator column can be specified by using the JavaX persistence discriminator column annotation on the root of the entity class hierarchy. The discriminatorType enumerated type is used to set the type of discriminator column in the database by setting the discriminatorType element of discriminator column to one of the defined types. DiscriminatorType is defined as, if discriminator column is not specified on the root of the entity hierarchy and a discriminator column is required, the persistence provider assumes a default column name of DTYPE and column name type of discriminatorType.string. The JavaX persistence discriminatorValue annotation may be used to set the value entered into the discriminator column for each entity in a class hierarchy.

You may decorate only concrete entity classes with discriminator value. If discriminator value is not specified on an entity in a class hierarchy that uses a discriminator column, the persistence provider will provide a default implementation specific value. If the discriminatorType element of discriminator column is discriminatorType STRING, the default value is the name of the entity. Table per concrete class strategy provides good support for polymorphic relationships between entities and queries that cover the entire entity class hierarchy. However, this strategy requires the columns that contain the state of subclasses to be nullable. In the table per concrete class strategy, which corresponds to inheritance type TABLE_PER_CLASS, each concrete class is mapped to a separate table in the database. All fields or properties in the class, including inherited fields or properties are mapped to columns in the classes table in the database.

This strategy provides poor support for polymorphic relationships and usually requires either SQL union queries or separate SQL queries for each subclass for queries that cover the entire entity class hierarchy. Support for this strategy is optional and may not be supported by all Java persistence API providers. The default java persistence API provider in the GlassFish server does not support this strategy. In the joined subclass strategy which corresponds to inheritancetype.joined, the root of the class hierarchy is represented by a single table and each subclass has a separate table that contains only those fields specific to that subclass. That is, the subclass table does not contain columns for inherited fields or properties. The subclass table also has a column or columns that represent its primary key, which is a foreign key to the primary key of the superclass table. This strategy provides good support for polymorphic relationships, but requires one or more joint operations to be performed when instantiating entity subclasses.

This may result in poor performance for extensive class hierarchies. Similarly, queries that cover the entire class hierarchy require join operations between the subclass tables resulting in decreased performance. Some Java persistence API providers including the default provider in the GlassFish server require a discriminator column that corresponds to the root entity when using the join subclass strategy. If you are not using automatic table creation in your application, make sure that the database table is set up correctly for the discriminator column defaults or use the discriminator column annotation to match your database schema. For information on discriminator columns, done. We have now finished learning about inheritance in entities. In the following video, we will look at managing entities and how we can use them. See you in the following video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics