Table-per-hierarchy strategy without a discriminator column in Hibernate
Solution 1:
SINGLE_TABLE
is a very powerful strategy but sometimes, and especially for legacy systems, you cannot add an additional discriminator column. For that purpose Hibernate has introduced the notion of discriminator formula: @DiscriminatorFormula
is a replacement of @DiscriminatorColumn
and uses an SQL fragment as a formula for discriminator resolution (no need to have a dedicated column).
@Entity
@DiscriminatorFormula("case when forest_type is null then 0 else forest_type end")
public class Forest { ... }