JPA Single Table Inheritance Example

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Till now we have learned about jpa entites and relationships between them,we simply mapped java entities to database tables. but what happens when inheritance comes into picture? How can we map entity hierarchies into database? There are various ways of mapping inheritance to database. Here, we will look into JPA single table inheritance example.

JPA Single Table Inheritance Example:

We have object model as depicted

Class diagram for vehicles
Class diagram for vehicles

This is what class diagram in this JPA single table inheritance example says:

  • Vehicle is an abstract class with fields idVehicle and manufacturer.
  • TransportationVehicle and PassengerVehicle are also abstract classes with fields loadCapacity and noOfPassengers respectively.
  • Truck, Car and Bike are concrete classes with noOfContainers, noOfDoors and saddleHeight as their respective properties.

For this name suggests, jpa single table inheritance example uses only one table to store all the fields of all classes that come in hierarchy. We have to create a database table as below.

Database table for vehicle inheritance
Database table for vehicle inheritance

It is obvious that

  • idVehicle and manufacturer columns correspond to Vehicle class and these fields are inherited by all subclasses.These columns will have values for all types of vehicles, i.e. Car, Truck and Bike
  • loadCapacity  is field of class TransportationVehicle and will be inherited by Truck. So all trucks will have a value in row for this column.
  • noOfPassengers is field of class PassengerVehicle and will be inherited by Car and Bike. So all cars and bikes will have a value for this column.
  • noOfContainers, noOfDoors and saddleHeight will have values that are only specific to Truck, Car and Bike respectively as these fields are unique to them.
  • There is an extra column called vehicle_type in table. This is used to identify the type of object in entity model. It will store values those are specific to classes. It is called as Discriminator column.

Discriminator Column:

It is used to identify the type of object in database. This is what javadoc says about @DiscriminatorColumn

Is used to define the discriminator column for the SINGLE_TABLE and JOINED inheritance mapping strategies.

The strategy and the discriminator column are only specified in the root of an entity class hierarchy or subhierarchy in which a different inheritance strategy is applied

For our Vehicle model, it is done using.

Let us directly move to jpa single table inheritance example and design java classes corresponding to object model and database shown in diagram above.

  •  Vehicle class is abstract but it is our root entity and inheritance starts from here, hence it needs to be annotated with @Inheritance@Inheritance(strategy = InheritanceType.SINGLE_TABLE)  specifies we are using single table strategy.
  • @DiscriminatorColumn(name = "VEHICLE_TYPE") specifies the name of discriminator column which stores the values to identify type of entity in database.
  • @GeneratedValue and @TableGenarator are used to create automatic id using table generator.

You will notice @MappedSuperClass annotation here:

MappedSuperClass annotation:

  • Classes annotated with @MappedSuperClass are like abstract classes in java
  • They are non persistent.
  • They cannot be queried over and are not mapped to any database table.
  • They are only used to contribute state and behavior to entities those are inherited from them.
  • It is good practice to mark abstract classes as @MappedSuperClass in jpa inheritance.
  • In our case, annotating TransportationVehicle and PassengerVehicle as @MappedSuperClass enables Truck, Car and Bike classes to hold their state.

 

Notice the @DisciriminatorValue annotation in this jpa single table inheritance example.

DiscriminatorValue annotation:

This is what javadoc says about @DiscriminatorValue.

Is used to specify the value of the discriminator column for entities of the given type. The DiscriminatorValue annotation can only be specified on a concrete entity class. If the DiscriminatorValue annotation is not specified and a discriminator column is used, a provider-specific function will be used to generate a value representing the entity type. If the DiscriminatorType is STRING, the discriminator value default is the entity name.

In our jpa single table inheritance example, we have specified discriminator value such that when a bike instance is persisted, database row corresponding to that instance will have value “Bike” in column vehicle_type. Similarly “Car” is persisted for car instance and “Truck” for a truck instance. Values from discriminator column i.e vehicle_type enables jpa identify the type of object.

Now let us test this jpa single table inheritance example:

persistence.xml:

Code to insert entities

Run this code and you will see data inserted into database

idVehicle manufacturer loadCapacity noOfContainers noOfPassengers noOfDoors saddleHeight vehicle_type
6 honda 1 30 Bike
7 lamborghini 2 2 Car
8 mercedes 100 2 Truck

The advantage of single table strategy is it does not require complex joins for retrieval and insertion of entities, but on the other hand it wastes database space as many columns need to be nullable and there isn’t any data for them.

I hope this article has helped understand single table strategy using jpa single table inheritance example. Please feel free to provide feedback and suggest improvements.

References for further study :

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

10 thoughts on “JPA Single Table Inheritance Example

Leave a Reply

Your email address will not be published. Required fields are marked *