Table des matières

Inheritance Mapping

We can map the inheritance hierarchy classes with the table of the database. There are several inheritance mapping strategies defined in the JPA specification :

  • MappedSuperclass : the parent classes, can’t be entities
  • Single Table : the entities from different classes with a common ancestor are placed in a single table
  • Joined Table : each class has its table and querying a subclass entity requires joining the tables
  • Table-Per-Class : all the properties of a class, are in its table, so no join is required

Every strategy results in a different database structure.

Because Hibernate is a JPA implementation, it contains all of the above.

MappedSuperclass

In the parent Class , we will use the @MappedSuperclass annotation, in this class we will not use the @Entity annotation

@MappedSuperclass
public class Person {

    @Id
    private long personId;
    private String name;

    // constructor, getters, setters
}

In the child Class we will use the @Entity annotation

@Entity
public class User extends Person {
    private String company;
    // constructor, getters, setters 
}

With this strategy, we will have one table User in the Data Base with three column : personId , name, company

Single Table

This strategy is the default one of the JPA specification , it will creates one table for each class hierarchy. So we will add @Inheritance in the parent Class

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Product {
    @Id
    private long productId;
    private String name;

    // constructor, getters, setters
}

In the child classes we will use the @Entity annotation

@Entity
public class Book extends Product {
    private String author;
}

@Entity
public class Pen extends Product {
    private String color;
}

Discriminator Values

In this strategy, all the entities will be in one table , so to differentiate between them we will use one column called DTYPE , this column will have the name of the entity as a value. Of course we can customize the value of this column using the annotation @DiscriminatorColumn.

@Entity(name="products")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="product_type", 
  discriminatorType = DiscriminatorType.INTEGER)
public class Product {
    // ...
}

And in the child entity we will use the @Entity annotation and @DiscriminatorColumn to customize the value of the DTYPE column

@Entity
@DiscriminatorValue("1")
public class Book extends Product {
    // ...
}

@Entity
@DiscriminatorValue("2")
public class Pen extends Product {
    // ...
}

In the parent we used DiscriminatorType.INTEGER to tell hibernate that the type of the DTYPE column will be an integer .

Joined Table

When we use this strategy, each entity will be mapped to its table. so the parent class will be like :

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Animal {
    @Id
    private long animalId;
    private String species;

    // constructor, getters, setters 
}

And child class like :

@Entity
public class Pet extends Animal {
    private String name;

    // constructor, getters, setters
}

The child table will have an animalId identifier column, this column can be customized using @PrimaryKeyJoinColumn(name = “pet_Id”)

Table per Class

When we use this strategy, hibernate will maps each entity to its table that will contains all the properties of the entity with the parent class properties. The parent class will be like :

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Vehicle {
    @Id
    private long vehicleId;

    private String manufacturer;

    // standard constructor, getters, setters
}

and the child class will be like

@Entity
public class Voiture extends Vehicle{
    ......
}

References : Baeldung

Laisser un commentaire