JPA Joined 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)

While studying inheritance in JPA entities, we have seen about JPA Single Table Inheritance in previous post. This will deal with another way of accomplishing it using jpa joined table inheritance example.

JPA Joined Inheritance Example:

Taking same example from single table inheritance, our data model is as below.

Class diagram for vehicles
Class diagram for vehicles

Class diagram for JPA joined 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.

Now let us create java classes for above diagram.

Vehicle.java

  • @Inheritance(strategy = InheritanceType.JOINED) specifies we are creating joined inheritance.
  • @DiscriminatorColumn(name = "VEHICLE_TYPE") specifies database column that stores information about type of entities. You can refer javadoc.
  • @TableGenerator and @GeneratedValue are used for automatic id creation in jpa.

TransportationVehicle.java

Truck.java

PassengerVehicle.java

Car.java

Bike.java

Things to notice here:

  • Vehicle is an abstract class but every vehicle needs to be identified by jpa hence it should have an id and hence it becomes an entity that has representation in database. So database will have a table mapped to Vehicle class.
  • TransportationVehicle and PassengerVehicle are abstract but we have mapped them as @MappedSuperClass. They will have not separate table mapped in database. But they have fields loadCapacity and noOfPassengers respectively. So we need to create them in database tables those are mapped to subclasses.
  • So, there will be a table named Truck with¬†column loadCapacity (represents¬† field inherited from mapped superclass TransportationVehicle) and noOfContainers.
  • A table named Car with column noOfPassengers(represents field inherited from mapped superclass PassengerVehicle) and noOfDoors.
  • A table named Bike with column noOfPassengers(represents field inherited from mapped superclass Passengervehicle) and saddleHeight.
  • Now there are four tables, Vehicle, Truck, Car and Bike. As the name JPA joined inheritance example obviously suggests, they need to be joined so that inheritance relationship is maintained.
  • Hence, subclass entities will have primary keys that will refer to primary keys of parent i.e. Vehicle class using foreign key relationship.

Now we will create database for this jpa joined inheritance example.

Database diagram for JPA joined inheritance example
Database diagram for JPA joined inheritance example
  • Vehicle table stores information about Vehicle class.
  • Truck table stores information about Truck class with inherited field from TransportationVehicle
  • Car and Bike tables store information about themselves and inherited field from PassengerVehicle
  • Primary keys of Truck, Car and Bike class are actually foreign keys that refer to primary keys of Vehicle. so subclass specific information can be accessed from superclass using foreign keys.

persistence.xml:

Let us test this jpa joined inheritance example.

This will insert data into tables as below.

Vehicle Table:

idvehicle manufacturer vehicle_type
9 honda Bike
10 lamborghini Car
11 volvo Truck

Bike Table:

idvehicle noOfPassengers saddleHeight
9 1 30

Car Table:

idvehicle noOfPassengers noOfDoors
10 2 2

Truck Table:

idvehicle loadCapacity noOfContainers
11 1000 2

The advantage of joined inheritance strategy is that it does not waste database space as in single table strategy. On the other hand, because of multiple joins involved for every insertion and retrieval, performance becomes and issue when inheritance hierarchies become wide and deep.

I hope this article helped understand jpa joined inheritance example.

 

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.

11 thoughts on “JPA Joined Inheritance Example

  • Pingback:JPA table per concrete class example | theJavaGeek

  • April 2, 2015 at 5:04 pm
    Permalink

    Please tell how fetch using find function.

    Reply
  • April 27, 2015 at 12:17 pm
    Permalink

    How to implement find because we don’t know whether primary key is of Truck class or bike class. if we give Vehicle class we cant get fields of subclass.

    Reply
  • May 25, 2016 at 1:23 am
    Permalink

    Don’t you need a @DiscriminatorValue specified for the entity sub-classes?

    Reply
    • May 25, 2016 at 9:09 am
      Permalink

      By default, a DiscriminatorValue will be supplied, in this case it is String representation of class. i.e. Bike, Truck, Car

      Reply
      • December 28, 2016 at 4:39 pm
        Permalink

        This is worth mentioning explicitly, otherwise the post doesn’t make sense

        Reply
  • June 14, 2016 at 12:44 am
    Permalink

    Awesome article , I’m checking the pros and cons of every inheritance method and this seems to be the most reliable

    Reply
  • March 30, 2017 at 1:39 am
    Permalink

    Hi Prasad,

    Please help me. I am having one question here. Suppose i have 3 radio buttons 1.Car, 2.Bike, 3.Both. So if i select car it will fetch all the car details if i select 2 it will only fetch the car details till here i am able to achieve but how to fetch both the Car and bike details if I select both.

    Parent class:
    @MappedSuperclass
    public abstract class BaseProsecutionDocument {

    private long dmsDocumentId;
    private long documentVersion;
    private String fileName;

    }

    Pros class:

    @Entity
    @Table(schema = “reference”, name = “prosecution_documents”)
    public class ProsDocument extends BaseProsecutionDocument {

    private Long id;
    private Long prosId;
    private Long ocportalSubmissionId;

    }

    Sumisiion class:

    @Entity
    @Immutable
    @Table(schema = “reference”, name = “submission_docs”)
    public class submissionDocument extends BaseProsecutionDocument {

    private Long id;
    private Long inventionId;

    }
    I want to know how to write the query for that..like i have written for those 2 radio buttons:

    public interface ProsecutionDocumentRepository extends JpaRepository {
    @Query(value = “SELECT ppd FROM ProsDocument ppd ” +
    “WHERE ppd.submissionId IN (SELECT p.id FROM submission p WHERE UPPER(p.doc) = UPPER(:doc)) ” +
    “AND ppd.documentType.documentType in (‘OFFICE’)”)
    Page findSubmissionOfficeDocumentsByDoc(@Param(“doc”) String docket, Pageable pageable);
    }

    Reply
  • September 10, 2018 at 11:49 am
    Permalink

    Such a good explanation man…
    Thank u so much

    Reply

Leave a Reply

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