liveBook · Manning

Discriminator column

As explained in Java Persistence with Spring Data and Hibernate epub

A discriminator is a stored value that identifies the concrete subtype represented by a row in a single-table inheritance mapping.

A discriminator column stores the concrete subtype represented by each row in a single-table inheritance mapping. A value such as `CC` identifies a `CreditCard` row among shared superclass and subclass columns. section 7.3

BillingDetails hierarchy

A JPA mapping declares `@Inheritance(strategy = InheritanceType.SINGLE_TABLE)` and `@DiscriminatorColumn(name = "BD_TYPE")` on `BillingDetails`; `CreditCard` supplies `@DiscriminatorValue("CC")`. Polymorphic queries read the hierarchy, while `CreditCard` queries add `where BD_TYPE='CC'`.

Using a discriminator column provides these practical tradeoffs:

  • Keeps polymorphic and non-polymorphic queries simple and performant, and supports straightforward ad hoc reporting without complex joins or unions. section 7.3
  • Requires subclass-specific columns to be nullable, so the database may not enforce required properties such as a credit-card expiration date. section 7.3
  • Does not make an abstract embeddable property polymorphic; without discrimination, a JPA provider cannot decide which concrete embeddable to load. section 7.6

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains annotation configuration, generated query filtering, formula-based discrimination, and nullable-column integrity tradeoffs.
Examples
Uses the BillingDetails and CreditCard hierarchy, BD_TYPE values, generated SQL filters, and a CARDNUMBER-based discriminator formula.
What it assumes
Assumes basic entity inheritance and persistence concepts, while demonstrating the concrete discriminator annotations from the mapping configuration.
What you can do after
Apply it via JPA annotations to configure subtype storage and interpret polymorphic or subtype-specific queries.
How it connects
Connects single-table inheritance with polymorphic queries, discriminator formulas, secondary tables, and embeddable-class polymorphism.

Related concepts

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.