JPA table per concrete class 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)

Continuing with our jpa inheritance strategies, this article provides jpa table per concrete class example and explains it using Vehicle model from previous article about joined inheritance.

JPA Table Per Concrete Class Example:

Object model used here is as below.

Class diagram for vehicles
Class diagram for vehicles

Class diagram for JPA table per concrete class 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.

Corresponding java classes for diagram above are:

Vehicle class

  •   @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) defines the strategy for jpa table per concrete class example.
  • Note that there is no @Table annotation for Vehicle as it is abstract class and will not have its representation in database.
  • @TableGenerator and @GeneratedValue are used for automatic id creation.

TransportationVehicle class

 Truck class

PassengerVehicle class

 Car class

 Bike class

You can notice in this jpa table per concrete class example:

  • Vehicle, TransportationVehicle and PassengerVehicle are abstract classes and they will not have separate table in database.
  • Truck, Car and Bike are concrete classes so they will be mapped to corresponding tables. These tables should include all the fields for classes annotated with @MappedSuperClass because they don’t have corresponding tables in database.
  • So, Truck table will have columns to store fields inherited from TransportationVehicle and Vehicle.
  • Similarly, Car and Bike will have columns to store fields inherited from PassengerVehicle and Vehicle.

Database for jpa table per concrete class example becomes as follows.

Database diagram for jpa table per concrete class example
Database diagram for jpa table per concrete class example

 

persistence.xml

Now that we’ve created database and persistence.xml, let us test this jpa table per concrete class example.

This inserts data into tables.
Truck table

idVehicle manufacturer loadCapacity noOfContainers
14 volvo 1000 2

Car table

idVehicle manufacturer noOfPassengers noOfDoors
13 lamborghini 2 2

Bike table

idVehicle manufacturer noOfPassengers saddleHeight
12 honda 1 30

I hope this jpa table per concrete class example helped.

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.

4 thoughts on “JPA table per concrete class example

  • June 15, 2016 at 6:29 pm
    Permalink

    The example explains it very well how to persist. But now I need to run a named query on the abstract entity, else I have copy that named query on all (concrete) POJOs.

    Currently I get an SQL error:
    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘jreports.abstractfoobar’ doesn’t exist

    The entity’s name (and class name) is AbstractFooBar.

    Reply
  • October 28, 2017 at 11:40 am
    Permalink

    Hi,

    can we use this “Vehicle” in a @OneToMany mapping with other Entity?

    example
    add “userId” column in “Vehicle” class.

    @Entity
    @Table(name = “UserDetails”)
    public class User {
    .
    .
    .
    .

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    @JoinColumn(name = “User_ID”)
    private Set ownedVehicles;
    .
    .
    .
    .
    }

    Reply
  • October 28, 2017 at 11:42 am
    Permalink

    small correction.

    private Set ownedVehicles;

    Reply

Leave a Reply

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