Home Table Per Class Inheritance
Post
Cancel

Table Per Class Inheritance

Table Per Class Inheritance :

In this strategy, each concrete class in the inheritance hierarchy is mapped to its own table in the database.

Example

Suppose we have an inheritance hierarchy consisting of the following classes:

Vehicle (parent class) Car (subclass of Vehicle) Bike (subclass of Vehicle)

Pojo Inheritance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public abstract class Vehicle {
  private Long id;
  private String manufacturer;
  private String model;

  // getters and setters
}

public class Car extends Vehicle {

  private Integer numDoors;

  // getters and setters
}

public class Bike extends Vehicle {

  private boolean electric;

  // getters and setters
}

Table Creation

Let’s start by defining the table structure that we’ll use for our example based on our example.

CREATE TABLE vehicle (
  id BIGINT NOT NULL,
  manufacturer VARCHAR(255),
  model VARCHAR(255),
  PRIMARY KEY (id)
);

CREATE TABLE car (
  id BIGINT NOT NULL,
  manufacturer VARCHAR(255),
  model VARCHAR(255),
  num_doors INTEGER,
  PRIMARY KEY (id),
);

CREATE TABLE bike (
  id BIGINT NOT NULL,
  manufacturer VARCHAR(255),
  model VARCHAR(255),
  is_electric boolean,
  PRIMARY KEY (id),
);


Note As we are using table per class inheritance strategy so all subclasses and superclass have their own table with their attributes and extended attributes..

Entity Class Hierarchy

Vehicle.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Entity
@Table(name = "vehicle")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) // Table Per Class strategy
public abstract class Vehicle {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "id")
    private Long id;

    @Column(name = "manufacturer")
    private String manufacturer;

    @Column(name = "model")
    private String model;

    // getters and setters
}

Car.java:

1
2
3
4
5
6
7
8
9
10
@Entity
@Table(name = "car")
public class Car extends Vehicle {

    @Column(name = "num_doors")
    private Integer numDoors;

    // getters and setters
}

Bike.java:

1
2
3
4
5
6
7
8
9
10
@Entity
@Table(name = "bike")
public class Bike extends Vehicle {

    @Column(name = "is_electric")
    private boolean electric;

    // getters and setters
}

Saving Entity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

Vehicle vehicle = new Vehicle();
vehicle.setManufacturer("Toyota");
vehicle.setModel("Camry");
session.save(vehicle);

Car car = new Car();
car.setManufacturer("Ford");
car.setModel("Mustang");
car.setNumDoors(2);
session.save(car);

Bike bike = new Bike();
bike.setManufacturer("Honda");
bike.setModel("CBR");
bike.isElectric(true);
session.save(bike);

tx.commit();
session.close();

Database Table View

Vehicle:

idmanufacturermodel
1ToyotaCamry

Car:

idmanufacturermodelnum_of_doors
1FordMustang2

Bike:

idmanufacturermodelis_electric
1HondaCBRtrue

Conclusion

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.

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