2.4.4. Inheritance
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 use a 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 { ... }
By default, when querying the top entities, Hibernate does not put a restriction clause on the discriminator column. This can be inconvenient if this column contains values not mapped in your hierarchy (through
@DiscriminatorValue
). To work around that you can use @ForceDiscriminator
(at the class level, next to @DiscriminatorColumn
). Hibernate will then list the available values when loading the entities.
You can define the foreign key name generated by Hibernate for subclass tables in the JOINED inheritance strategy.
@Entity @Inheritance(strategy = InheritanceType.JOINED) public abstract class File { ... } @Entity @ForeignKey(name = "FK_DOCU_FILE") public class Document extends File {
The foreign key from the
Document
table to the File
table will be named FK_DOCU_FILE
.