@DiscriminatorColumn is the JPA annotation that names the column used to tell entity types apart when a class hierarchy is stored in one table. It belongs on the root entity of a SINGLE_TABLE (or JOINED) inheritance mapping. Each row stores a discriminator value, set with @DiscriminatorValue on each subclass, and the persistence provider reads it to create the right Java class.
If you omit it, JPA uses a column called DTYPE of type string.
With the SINGLE_TABLE strategy, Person, Employee and Customer all share one table. Without a marker, the provider could not know whether a row should become an Employee or a Customer. The discriminator column holds that marker, and Hibernate adds it to inserts and uses it in the where clause when you query a subclass.
| Attribute | Default | Purpose |
|---|---|---|
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 |
All of these attributes have been available since JPA 1.0.
The generated table looks like this:
Subclass columns must be nullable in this strategy, because rows of other types leave them empty. That is the main trade-off of SINGLE_TABLE: fast queries and no joins, but a wide, sparse table and weaker not-null constraints.
A string is readable but takes more space. For large tables you can use an integer or a single character:
The value in @DiscriminatorValue is always written as a string literal, and JPA converts it according to the declared discriminatorType. Mixing a type with an incompatible value, such as "abc" with INTEGER, fails at start-up.
SINGLE_TABLE: the discriminator column is required in practice, and it is the case most developers meet.JOINED: each class has its own table, so the discriminator is optional. The JPA specification allows one, but support differs between providers and versions, and the joins already identify the type.TABLE_PER_CLASS: no discriminator, because each concrete class has its own table.If you rely on the provider's default behaviour with JOINED, check the documentation of your Hibernate version rather than assuming a column exists.
Hibernate offers @DiscriminatorFormula to compute the type from an expression instead of a stored column, useful for legacy schemas. @DiscriminatorOptions(force = true) makes Hibernate add the discriminator restriction even when you query the root entity, which matters when the table contains rows that do not map to any entity. Both are Hibernate-specific, not part of JPA, so they tie your code to that provider.
@DiscriminatorColumn on a subclass; it belongs on the root of the hierarchy.@DiscriminatorValue and relying on the class name, which breaks when you rename a class.length too small for a longer value, leading to truncation or insert errors.SINGLE_TABLE mapping.Pick stable, short values and treat them as part of your database contract.
DTYPE, with type STRING and length 31, when no @DiscriminatorColumn is declared.
No. The defaults apply when it is missing, but declaring it makes the schema explicit and lets you choose the name and type.
Mapping it as a read-only field is possible with @Column(insertable = false, updatable = false), but it is rarely needed. The provider manages the value for you.
The column defines where the type marker is stored. The value defines what is stored for each entity class.
@DiscriminatorColumn defines the column that stores the entity type in an inheritance hierarchy, with name, discriminatorType and length as the main options. Place it on the root entity, pair it with @DiscriminatorValue on each class, and choose short, stable values. It is central to SINGLE_TABLE mapping and optional for the other strategies.