@DiscriminatorColumn tells JPA which column stores the type marker (the discriminator) that distinguishes entity subclasses in an inheritance hierarchy. When several classes share one table, or when the provider needs to know the concrete type of a row, it reads this column to decide which class to instantiate. You place it on the root entity of the hierarchy, usually together with @Inheritance and @DiscriminatorValue.
With the SINGLE_TABLE inheritance strategy, all classes of a hierarchy are stored in one table. A row for a CreditCardPayment and a row for a BankTransferPayment look alike, so the table needs one extra column that says which class each row belongs to. That column is the discriminator.
The annotation lives in jakarta.persistence (older code uses javax.persistence).
The resulting table has id, amount, payment_type, card_last4, and iban. Rows for card payments contain CARD in payment_type, and the subclass-specific columns of the other type stay NULL.
| Attribute | Default | Meaning |
|---|---|---|
name | DTYPE | Column name |
discriminatorType | STRING | STRING, CHAR, or INTEGER |
length | 31 | Length for STRING columns |
columnDefinition | provider default | Raw SQL fragment for the column |
options | empty | Extra DDL options for the column (JPA 3.2) |
If you omit the annotation, JPA still creates a DTYPE string column for SINGLE_TABLE. Setting it explicitly gives you a meaningful name and a controlled length.
@DiscriminatorValue on each entity class sets the value stored in the column. Without it, the default for STRING is the entity name.
Integer or single-character codes are compact and index well, but strings are easier to read when you inspect the database. Whichever type you choose, keep the values stable, because changing them means migrating existing rows.
| Strategy | Discriminator used? |
|---|---|
SINGLE_TABLE | Required. Provided by @DiscriminatorColumn or the DTYPE default |
JOINED | Optional in the JPA spec. Hibernate does not need one and determines the type by joining tables; some providers, such as EclipseLink, use it |
TABLE_PER_CLASS | Not used. Each concrete class has its own table |
In Hibernate 6, a discriminator column for JOINED is ignored unless you explicitly opt in using Hibernate-specific settings, so do not rely on it for portable code. Check your provider's documentation if you need it.
The discriminator is what makes polymorphic queries work. A query on the root class returns all subclasses, while a query on a subclass adds a condition on the discriminator.
The second query makes Hibernate add where payment_type = 'CARD'. The type(p) function in the third query lets you filter on the class in JPQL without referring to the column.
CHECK constraint or an enum-like lookup if you need to prevent invalid values.SINGLE_TABLE must be nullable, because rows of other types leave them empty. This is the main trade-off against the JOINED strategy.@Column(name = "payment_type", insertable = false, updatable = false) to keep it read-only.@DiscriminatorFormula for deriving the type from an expression on legacy tables that have no dedicated column, and @DiscriminatorOptions(force = true) for adding the condition to every query on a root that has non-hierarchy rows.@DiscriminatorValue values in sync with the data.Not strictly. If you use SINGLE_TABLE without it, JPA creates a string column called DTYPE. Add the annotation to control name, type, and length.
On the root entity of the inheritance hierarchy only, not on the subclasses.
@DiscriminatorColumn defines the column on the root class. @DiscriminatorValue defines the value each concrete class stores in that column.
Not directly. Use STRING or INTEGER values that match your enum's code, and keep the mapping in sync manually.
@DiscriminatorColumn configures the column that identifies the concrete entity type in an inheritance hierarchy, most importantly with SINGLE_TABLE. Combine it with @Inheritance on the root and @DiscriminatorValue on each class, pick a stable compact type, and remember that the trade-off is nullable subclass columns in one wide table.