@DiscriminatorValue sets the value that JPA stores in the discriminator column to identify which entity subclass a row belongs to. It matters with SINGLE_TABLE and JOINED inheritance, where JPA must tell subclasses apart when it loads a row. If you leave it out, the default is the entity name for string discriminators.
In Spring Boot 3 and Hibernate 6 the annotation lives in the jakarta.persistence package. Older Spring Boot 2 projects use javax.persistence with the same behaviour.
With inheritance mapping, several entity classes share a table (single table) or share a root table (joined). To build the right Java object from a row, the persistence provider needs a marker. That marker is the discriminator column. The root entity declares the column with @DiscriminatorColumn, and each subclass declares its marker value with @DiscriminatorValue.
When you save a Car, Hibernate writes the car's discriminator value into the column. When you query for Vehicle, Hibernate reads the column and instantiates the correct subclass for each row.
Hibernate creates one table with all columns from every class plus vehicle_type.
Car rows have NULL in payload_tons and Truck rows have NULL in number_of_doors. That is why subclass columns in a single table must be nullable, so avoid nullable = false on them.
@DiscriminatorValue takes a single String that is converted according to the discriminator type declared on the root.
| Discriminator type | Default when @DiscriminatorValue is missing | Notes |
|---|---|---|
STRING (default) | The entity name, which defaults to the unqualified class name | Readable, slightly larger rows |
CHAR | Provider specific, so always set a value | Single character such as C |
INTEGER | Provider specific, so always set a value | Compact, written as "1", "2" |
If you do not set @DiscriminatorColumn at all, the column is named DTYPE, uses STRING, and has length 31 by default. Setting the root's own @DiscriminatorValue lets a concrete root class be stored and queried too. An abstract root never needs one.
Relying on the default value ties your data to the Java class name. If someone renames Car to Automobile during a refactor, rows already stored as Car no longer match any entity and Hibernate throws an error when loading them. Explicit short values such as CAR decouple your schema from class names and keep the column small.
Choose values once and treat them as part of the database contract. Document them, and add a migration if you ever have to change them.
Polymorphic queries work without touching the discriminator yourself.
Spring Data JPA translates the query on Car into a WHERE vehicle_type = 'CAR' clause. You can also write SELECT v FROM Vehicle v WHERE TYPE(v) = Car in JPQL, which compares the discriminator for you, so avoid hard-coding the column value in queries.
| Strategy | Tables | Discriminator needed | Trade-off |
|---|---|---|---|
SINGLE_TABLE | One table for the hierarchy | Yes | Fast queries, no joins, but sparse nullable columns |
JOINED | One table per class | Optional in JPA, Hibernate infers type from joins | Normalised, but needs joins |
TABLE_PER_CLASS | One table per concrete class | No | Polymorphic queries use UNION, so they are slower |
The JPA specification makes the discriminator optional for JOINED. Hibernate does not require one there, but it honours a discriminator if you declare it. For TABLE_PER_CLASS it is not used.
NOT NULL, which makes inserts of other subclasses fail."ELECTRIC_VEHICLE" in a column of length 10.javax.persistence in a Spring Boot 3 project, which does not compile against the Jakarta APIs.@DiscriminatorValue required?No. If you omit it, JPA uses the entity name for STRING discriminators. For CHAR and INTEGER you should always supply it, and explicit values are safer for every type.
Yes. The root can have its own value, which is needed when you instantiate and persist the root as a concrete class.
The root entity defines it with @DiscriminatorColumn. Without that annotation JPA uses a DTYPE string column.
Yes. Spring Data JPA uses the underlying JPA provider, so repositories for the root or a subclass return the right types automatically.
@DiscriminatorValue gives each subclass a stable marker in the discriminator column so JPA can load the correct entity type from shared tables. Declare the column on the root with @DiscriminatorColumn, set short explicit values on every subclass, keep subclass columns nullable in single table mapping, and import from jakarta.persistence on current Spring Boot versions.