Quick revision
The short answer
SINGLE_TABLE, the default, stores the whole hierarchy in one table with a discriminator column: polymorphic queries are fast, but subclass columns must be nullable. JOINED gives each class its own table joined by primary key: the schema is normalized, but polymorphic queries need joins. TABLE_PER_CLASS gives each concrete class a full table: polymorphic queries need UNION and IDENTITY id generation cannot be used. @MappedSuperclass only shares mapped fields such as audit columns; it is not an entity and cannot be queried polymorphically. We choose by query patterns, not by the shape of the class hierarchy.
What to remember
In this answer, we compared the inheritance strategies. SINGLE_TABLE uses one table with a discriminator, giving fast polymorphic queries at the price of nullable subclass columns. JOINED normalizes into a table per class and pays with joins on polymorphic queries. TABLE_PER_CLASS gives each concrete class a full table, needs UNION for polymorphic queries and cannot use IDENTITY ids. @MappedSuperclass shares mapped fields without being an entity. The right choice follows the query patterns, not the class diagram.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
SINGLE_TABLE, the default, stores the whole hierarchy in one table with a discriminator column: polymorphic queries are fast, but subclass columns must be nullable. JOINED gives each class its own table joined by primary key: the schema is normalized, but polymorphic queries need joins. TABLE_PER_CLASS gives each concrete class a full table: polymorphic queries need UNION and IDENTITY id generation cannot be used. @MappedSuperclass only shares mapped fields such as audit columns; it is not an entity and cannot be queried polymorphically. We choose by query patterns, not by the shape of the class hierarchy.
Why this question is asked
Inheritance is natural in Java and has no direct equivalent in relational tables, so JPA offers several ways to map it, and each one makes a different set of queries fast or slow. Picking the one that mirrors the class diagram most elegantly is a common mistake that shows up later as wide joins or null-filled tables. Interviewers ask for this comparison because it reveals whether we think about how the data will be queried. In this answer, we will recap the problem with a small hierarchy, map it with each strategy and look at the SQL, see where @MappedSuperclass fits, and finish with how to choose.
1A Quick Recap: The Problem Inheritance Mapping Solves
Suppose we have an abstract class Payment with id, amount and createdAt, and two subclasses: CardPayment adds cardLast4, and BankTransfer adds iban. In Java we can write a query for all payments and get back a mix of both types; this is called a polymorphic query. In the database there are only tables and columns. An inheritance strategy decides how the three classes become tables, and therefore how a polymorphic query, and a query for one subtype, become SQL.
2SINGLE_TABLE: One Table and a Discriminator
SINGLE_TABLE is the default when we put @Inheritance on the root without a strategy. All classes share one table, and a discriminator column, DTYPE by default, records which class each row is.
Polymorphic queries are as fast as they can be: one table, no joins, and one index serves all types. The catch is that a column belonging to one subclass is empty for the others, so card_last4 must be nullable even if every card payment requires it. We can restore integrity with check constraints per discriminator value, but the database will not do it for us automatically.
3JOINED: A Table per Class
With JOINED, the root table holds the shared columns, and each subclass has its own table with only its columns, whose primary key is also a foreign key to the root.
The schema is normalized, and subclass columns can be NOT NULL. But a polymorphic query must outer join every subclass table, so a hierarchy with 12 subclasses means 12 joins for a simple list. Inserts write two rows, one in the root table and one in the subclass table. Queries on a single subclass are cheaper, one join to the root.
4TABLE_PER_CLASS: A Full Table per Concrete Class
With TABLE_PER_CLASS, each concrete class gets a complete table containing both the inherited and its own columns, and there is no table for the abstract root.
Queries for one subclass are simple and fast, a single table. Polymorphic queries need a UNION across all tables, which is expensive and hard to index, and a @ManyToOne pointing to the abstract Payment cannot be backed by a real foreign key. Ids must be unique across all the tables, so IDENTITY columns, which are per table, cannot be used; we need a shared sequence or another generator.
5@MappedSuperclass: Sharing Fields Without a Hierarchy
Sometimes we only want to reuse mapped fields, such as id, createdAt and updatedBy, across unrelated entities. @MappedSuperclass does exactly that: its mapped fields are copied into each subclass's table, but the superclass is not an entity.
| Strategy | Tables | Polymorphic query | Subclass columns | Notes |
|---|---|---|---|---|
SINGLE_TABLE | one | fast, no joins | nullable | default; check constraints help |
| JOINED | one per class | outer joins to all | can be NOT NULL | two inserts per entity |
TABLE_PER_CLASS | one per concrete class | UNION ALL | can be NOT NULL | no IDENTITY ids, no FK to root |
@MappedSuperclass | one per entity | not possible | normal columns | shares fields only |
That is the right tool for shared technical columns. It is not a substitute for entity inheritance when we need to query or associate with the base type.
6Choosing by Query Patterns
The deciding question is how the data is read. If most queries list all types together, such as all payments for an account, SINGLE_TABLE is usually best, with check constraints to protect subclass columns. If queries mostly target one subclass and subclass data is large or rarely read with the others, JOINED keeps the root table narrow. TABLE_PER_CLASS fits when subtypes are almost always queried separately and polymorphic queries are rare. And if there is no polymorphic querying at all, perhaps inheritance is not needed in the mapping, and @MappedSuperclass or composition is simpler.
7Follow-up questions
Can we change the strategy later?
Yes, but it is a data migration, since the tables change shape. Moving from JOINED to SINGLE_TABLE, for example, means creating the wide table, copying rows with their discriminator, switching reads and dropping the old tables. That is why the choice deserves thought up front.
Does SINGLE_TABLE waste a lot of space?
Null columns are cheap in most databases; PostgreSQL stores them in a null bitmap. The bigger cost of a very wide table is readability and integrity, not storage, which is why check constraints matter.
Can a hierarchy mix strategies?
JPA allows only one strategy per hierarchy root. Hibernate supports a secondary table for a subclass in SINGLE_TABLE, which gives a similar effect for one large subclass, but the mapping gets harder to reason about, so use it sparingly.
8Conclusion
In this answer, we compared the inheritance strategies. SINGLE_TABLE uses one table with a discriminator, giving fast polymorphic queries at the price of nullable subclass columns. JOINED normalizes into a table per class and pays with joins on polymorphic queries. TABLE_PER_CLASS gives each concrete class a full table, needs UNION for polymorphic queries and cannot use IDENTITY ids. @MappedSuperclass shares mapped fields without being an entity. The right choice follows the query patterns, not the class diagram.
Open with
SINGLE_TABLE is the default. The whole hierarchy goes into one table with a discriminator column, DTYPE unless you rename it, so polymorphic queries like all payments are a single select with no joins and one index works for every type.
Then, if they want more
The cost is that subclass-specific columns have to be nullable, so I'd add check constraints per discriminator value for integrity. JOINED gives each class its own table, with the subclass primary key also a foreign key to the root.
It's normalized and subclass columns can be NOT NULL, but a polymorphic query outer joins every subclass table, and every insert writes two rows, which gets expensive with many subclasses. TABLE_PER_CLASS gives each concrete class a complete table with the inherited columns repeated.
Querying one subtype is simple, but polymorphic queries need a UNION, you can't have a real foreign key to the base type, and you can't use IDENTITY generation because ids must be unique across tables, so you need a sequence. @MappedSuperclass is different: it just shares mapped fields, like audit columns, into each entity's table, but it's not an entity, so you can't query it or associate to it.
I choose based on how the data is queried: mostly polymorphic listing points to SINGLE_TABLE, mostly per-subtype access with large subtype data points to JOINED.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare SINGLE_TABLE, JOINED and TABLE_PER_CLASS inheritance strategies and...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/e24374d08b/