liveBook · Manning

@DiscriminatorFormula

As explained in Java Persistence with Spring Data and Hibernate epub

@DiscriminatorFormula is a Hibernate annotation that derives an entity subtype discriminator from an SQL expression instead of a stored discriminator column.

@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

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Defines an SQL CASE expression and discriminator literals, but does not describe formula lifecycle or performance.
Examples
Uses CARDNUMBER to derive CC or BA for CreditCard and BankAccount rows.
What it assumes
Assumes single-table inheritance and SQL CASE expressions; distinguishes the Hibernate extension from the JPA specification.
What you can do after
Configure Hibernate to derive entity subtypes from existing row values without adding a discriminator column.
How it connects
Connects single-table inheritance, discriminator columns, subclass mappings, Hibernate extensions, and SQL CASE expressions.

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.