Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Compare SINGLE_TABLE, JOINED and TABLE_PER_CLASS inheritance strategies and @MappedSuperclass.

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.

Java
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
abstract class Payment { @Id @GeneratedValue Long id; BigDecimal amount; }
@Entity @DiscriminatorValue("CARD") class CardPayment extends Payment { String cardLast4; }
@Entity @DiscriminatorValue("BANK") class BankTransfer extends Payment { String iban; }
// table payment(id, type, amount, card_last4, iban)
em.createQuery("from Payment", Payment.class).getResultList();
// select ... from payment (no joins)
em.createQuery("from CardPayment", CardPayment.class).getResultList();
// select ... from payment where type = 'CARD'

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.

Java
@Entity @Inheritance(strategy = InheritanceType.JOINED)
abstract class Payment { ... }
// tables payment(id, amount), card_payment(id, card_last4), bank_transfer(id, iban)
em.createQuery("from Payment", Payment.class).getResultList();
// select ... from payment p
// left join card_payment c on c.id = p.id
// left join bank_transfer b on b.id = p.id

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.

Java
@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
abstract class Payment { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) Long id; ... }
// tables card_payment(id, amount, card_last4), bank_transfer(id, amount, iban)
em.createQuery("from Payment", Payment.class).getResultList();
// select ... from (select ... from card_payment union all select ... from bank_transfer)

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.

Java
@MappedSuperclass
abstract class Auditable {
@CreatedDate Instant createdAt;
@LastModifiedBy String updatedBy;
}
@Entity class Invoice extends Auditable { @Id Long id; ... } // invoice(id, created_at, updated_by, ...)
@Entity class Customer extends Auditable { @Id Long id; ... } // customer(id, created_at, updated_by, ...)
// em.createQuery("from Auditable") -> not allowed in JPA: Auditable is not an entity
Inheritance strategies side by side
StrategyTablesPolymorphic querySubclass columnsNotes
SINGLE_TABLEonefast, no joinsnullabledefault; check constraints help
JOINEDone per classouter joins to allcan be NOT NULLtwo inserts per entity
TABLE_PER_CLASSone per concrete classUNION ALLcan be NOT NULLno IDENTITY ids, no FK to root
@MappedSuperclassone per entitynot possiblenormal columnsshares fields only
Each strategy makes a different query cheap.

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.

What to say in the interviewAbout 90 seconds aloud

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.
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/