Multi-layered inheritance strategies with JPA / Hibernate

Viewed 38

Is it possible to mix inheritance types if they are not on the same level using hibernate / JPA?

The names below are just examples, but JPA seems to expect tables for leaf and flower even though their closest inheritance type is SINGLE_TABLE.

I am pretty sure this is not supported, but arguably, why not?

I am using Hibernate 5.4.2.Final

@Entity
@Table
@Inheritance(strategy = JOINED)
abstract class Trunk {
    @Id private Long id;

    ...
}
@Entity
@Inheritance(strategy = SINGLE_TABLE)
@DiscriminatorColumn(name = "kind")
abstract class Branch extends Trunk {
    private String kind;

    ...
}
@Entity
@DiscriminatorValue(value = "leaf")
class Leaf extends Branch {
    ...
}


@Entity
@DiscriminatorValue(value = "flower")
class Flower extends Branch {
    ...
}

The schema would be something like this:

+-----------+     +-----------+
| Trunk     |     | Branch    |
+-----------+     +-----------+
| id:bigint |1---1| id:bigint |
| ...       |     | kind:text |
+-----------+     | ...       |
                  +-----------+
0 Answers
Related