@DiscriminatorFormula maps single-table inheritance when the schema has no discriminator column. Hibernate evaluates an SQL expression for each row and uses its result to select the entity subtype. section 7.3
Deriving the subtype from card data
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @org.hibernate.annotations.DiscriminatorFormula( "case when CARDNUMBER is not null then 'CC' else 'BA' end" ) public abstract class BillingDetails { } The expression returns CC for credit-card rows and BA for bank-account rows; subclass mappings declare those discriminator values. section 7.3
Use the formula when:
- An existing single table cannot accommodate an additional discriminator column. section 7.3
- The subtype can be identified reliably from values already stored in each row. section 7.3
