The @Inheritance annotation tells JPA how to map an entity class hierarchy to database tables. You put it on the root entity, and its strategy attribute selects one of three mappings: SINGLE_TABLE, JOINED or TABLE_PER_CLASS. If you omit it, the default is SINGLE_TABLE.
The choice affects query speed, schema design and data integrity, so it is worth making deliberately. Examples below use the jakarta.persistence package (JPA 3.x, Hibernate 6, Spring Boot 3); on older stacks the package is javax.persistence.
Object-oriented classes support inheritance, but relational tables do not. JPA bridges the gap with three strategies defined by the InheritanceType enum. The annotation goes on the root of the hierarchy only, and subclasses inherit the setting.
A hierarchy can also include a @MappedSuperclass, which shares fields but is not an entity and cannot be queried polymorphically. Use @Inheritance when you need to query the parent type and get subtypes back.
All classes in the hierarchy share one table. A discriminator column records the concrete type of each row.
| id | vehicle_type | model | doors | has_pedals |
|---|---|---|---|---|
| 1 | CAR | Civic | 4 | null |
| 2 | BIKE | Roadster | null | true |
Polymorphic queries need no joins, so this is the fastest option. The costs are that subclass columns must be nullable, so you cannot declare NOT NULL on them, and the table can become wide and sparse.
Each class gets its own table. The root table holds shared columns, and each subclass table holds its own columns plus a primary key that is also a foreign key to the parent.
The schema is normalised and columns can be NOT NULL. Loading a CardPayment requires a join with Payment, and polymorphic queries on the root join or union all subclass tables, so reads are slower than with SINGLE_TABLE. A discriminator column is optional here; Hibernate does not need one for JOINED, but you can add one.
Each concrete class gets a full table containing all inherited and own columns. There is no shared parent table.
Querying Document forces a UNION across all subclass tables, which is slow on large data. IDs must be unique across tables, so GenerationType.IDENTITY does not work; use a sequence or table generator. The JPA specification makes support for this strategy optional, so it is the least portable choice and rarely recommended.
| Concern | SINGLE_TABLE | JOINED | TABLE_PER_CLASS |
|---|---|---|---|
| Polymorphic query speed | Best | Joins needed | Union needed |
| Normalised schema | No | Yes | Partly |
NOT NULL on subclass columns | No | Yes | Yes |
| Discriminator column | Required | Optional | None |
| Typical use | Few extra fields | Many subclass fields | Rarely used |
As a rule, start with SINGLE_TABLE for shallow hierarchies with few subclass-specific columns, and move to JOINED when data integrity or table width becomes a problem. Also consider whether composition would be simpler than inheritance.
SINGLE_TABLE. If you do not annotate the root entity with @Inheritance, JPA maps the whole hierarchy to one table.
For SINGLE_TABLE a discriminator is always used; if you omit the annotation, the column is named DTYPE by default. Declare it to control the name and type.
No. The strategy is set once on the root entity of the hierarchy.
Yes. select v from Vehicle v returns Car and Bike instances. Abstract roots work fine as query targets.
@Inheritance selects how a JPA entity hierarchy maps to tables. SINGLE_TABLE is fast but sparse, JOINED is clean and normalised at the cost of joins, and TABLE_PER_CLASS is rarely worth it. Put the annotation on the root entity, use jakarta.persistence imports on current stacks, and pick the strategy based on your queries and integrity needs.