Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

Object-relational mapping encourages modelling the database around class hierarchies. After years of experience with inheritance-heavy JPA models, what modelling principles would you set for long-lived systems?

For long-lived systems I would model the database for the data and the queries first and let the classes adapt, prefer composition and an explicit type column over deep entity inheritance, and keep any hierarchy shallow with a documented reason for choosing SINGLE_TABLE or JOINED. The tables will outlive the code and will be read by reports and systems that never see our class hierarchy, so new hierarchies should pass a design review before they spread.

Why this question is asked

Inheritance looks free when we write it: one extends keyword and JPA works out the tables. The cost arrives years later, when a report needs one simple query and has to join five tables, or when a new team in another language reads a table full of columns that only make sense for one subclass. Interviewers ask this at the senior level because the answer shows whether we think about data as something that outlives the application. In this answer, we will recap how JPA maps inheritance, look at what goes wrong in long-lived systems, and then go through the principles I would set, with an example of applying them.

1A Quick Recap of JPA Inheritance Mapping

JPA lets an entity class extend another entity class and offers three strategies for storing the hierarchy. SINGLE_TABLE puts every subclass in one table with a discriminator column, a column whose value says which subclass a row belongs to. JOINED gives the base class one table and each subclass its own table that shares the primary key, so reading a subclass means joining. TABLE_PER_CLASS gives every concrete class a full table of its own, and a query on the base type becomes a UNION across all of them.

There is also @MappedSuperclass, which is not entity inheritance at all. It only copies shared fields such as id, createdAt and version into each entity's own table, and nobody can query the superclass. This difference matters later, because most of the reuse people want from inheritance can be had from @MappedSuperclass or from embeddables without making the database depend on a class tree.

The inheritance options at a glance
OptionTablesCost that appears later
SINGLE_TABLEone table, discriminator columnmany nullable columns, NOT NULL hard to enforce
JOINEDbase table plus one per subclassjoins on every read, deep trees mean many joins
TABLE_PER_CLASSone full table per concrete classUNION queries, ids must be unique across tables
@MappedSuperclasseach entity has its own tableno polymorphic queries, which is usually fine
Composition plus typeone table, explicit type columnthe type logic lives in code we write ourselves
Each choice is a trade between query simplicity, constraints and how faithfully the classes are mirrored.

2What Goes Wrong in Systems That Live for Years

Picture a payments system started ten years ago. Payment had subclasses CardPayment, BankTransfer and WalletPayment, mapped with JOINED, and later CardPayment grew RecurringCardPayment and ThreeDsCardPayment. A finance analyst who wants all payments of last month with their method-specific fields now needs a query with five outer joins, and the analyst does not know which tables to join because the hierarchy lives only in Java code.

The same model with SINGLE_TABLE would have had its own problem: forty columns, most of them null for any given row, and no way to say in the database that card_last4 is required for card payments, because the column must be nullable for every other type. The reason both go wrong is the same. The database was shaped to mirror a class tree that made sense to one team at one time, and the tree kept growing because adding a subclass was the easiest change to make.

Notice also who suffers. The original Java code works well with either mapping. The pain lands on reporting tools, data pipelines, a second service written in another language, and the engineers who migrate the data years later. That is the core reason to put the data first.

3The Principles I Would Set

The first principle is that we design the tables for the data and for the queries we know we will run, and then write classes that fit those tables. If the most common query is all payments for a merchant by date, the table that answers it should be obvious and cheap to read without knowing the Java model.

The second principle is composition over inheritance. Instead of a subclass per payment method, I would keep one payment table with an explicit method column and put method-specific details in embeddables or in small child tables keyed by payment id. The type becomes a plain value that any SQL user can filter on, and behaviour that differs by type moves into strategy classes in code, which are easy to change without a migration.

The third principle is that when inheritance really fits, the hierarchy stays shallow, usually one level, and the strategy is chosen on purpose and written down. SINGLE_TABLE is a good choice when subclasses differ by a few columns and we query across types often. JOINED fits when subclasses carry many distinct and required columns and we mostly read one type at a time. TABLE_PER_CLASS rarely survives a review. The written reason matters because the next team needs to know whether the choice is still valid.

The fourth principle is that new entity hierarchies go through design review before they are merged. Once a hierarchy has data in production, removing it is a migration project, so the cheapest time to question it is in the pull request that introduces it.

4Applying Them to an Example

Here is the payments model rewritten along those lines. The method is an explicit column, card details are an embeddable that is null for other methods, and a check constraint keeps the rules in the database where every reader benefits from them.

Java
@Entity
@Table(name = "payment")
class Payment {
@Id @GeneratedValue Long id;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
PaymentMethod method; // CARD, BANK_TRANSFER, WALLET
BigDecimal amount;
@Embedded CardDetails card; // null unless method = CARD
}
-- in the migration
ALTER TABLE payment ADD CONSTRAINT card_needs_last4
CHECK (method <> 'CARD' OR card_last4 IS NOT NULL); -- rule visible to every reader

So the analyst's query becomes one table with a filter on method, a Python pipeline can read the table without a mapping library, and adding a new method is a new enum value plus perhaps an embeddable, not a new table in a tree. The code still has polymorphism where it helps: a PaymentHandler interface with one implementation per method, chosen by the method value.

5Rolling the Principles Out

Principles only help if teams can follow them without a debate each time. I would write them as a short modelling guide with this example in it, add a question about inheritance to the design review checklist, and agree that existing hierarchies are left alone until a real need, such as a reporting project or a migration, gives us a reason to flatten them. Rewriting working models only for purity costs more than it returns.

I would also measure the outcome: how many entity hierarchies exist, how deep they are, and which tables the data team complains about. Those numbers tell us whether the guide is working better than opinions do.

Step throughReviewing a proposed entity hierarchy
  1. Does a type column plus composition work?most cases
    YesUse it, no hierarchyNoConsider inheritance
  2. More than one level deep?
    YesFlatten or redesignNoChoose a strategy
  3. Few extra columns, cross-type queries?
    YesSINGLE_TABLENoJOINED
  4. Write the reason in the design docso the next team can revisit it

4 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Is inheritance in JPA ever the right answer?

Yes, when the subclasses really share identity and most queries treat them as one kind of thing. A shallow SINGLE_TABLE hierarchy for two or three notification types that differ by a couple of columns works well for years. The problems come from depth, from many subclasses, and from choosing a strategy without thinking about who else reads the table.

What would you do with an existing deep hierarchy that hurts reporting?

I would not rewrite it at once. A database view that flattens the hierarchy gives reporting a simple shape quickly and safely. If the hierarchy also slows development, I would migrate it in steps with expand and contract: add the flat structure, backfill, move reads, then drop the old tables, one subclass at a time.

How do you handle shared fields like createdAt and version without inheritance?

With @MappedSuperclass for technical fields that every entity has, or with an @Embeddable such as AuditInfo. Neither creates a polymorphic relationship between tables, so the database stays simple and each table keeps its own columns.

7Conclusion

In this answer, we saw that JPA inheritance mirrors a class tree in the database, and that over years this tree hurts the people who read the data without our code. The principles are to design tables for data and queries first, prefer composition and an explicit type column, keep any hierarchy shallow with a written reason for SINGLE_TABLE or JOINED, and review new hierarchies before they spread.

What to say in the interviewAbout 90 seconds aloud

Open with

After working with inheritance-heavy JPA models for years, my main lesson is that the data outlives the code. The tables will be read by reports, pipelines and future services that never see our class hierarchy, so I would set a principle that we design the tables for the data and the queries first and let the classes adapt to them.

Then, if they want more

In practice that means composition over inheritance: one table with an explicit type column, method-specific details in embeddables or small child tables, and polymorphism in code through strategy classes rather than through the mapping.

When inheritance really fits, I would keep the hierarchy shallow, usually one level, and choose the strategy deliberately: SINGLE_TABLE when subclasses differ by a few columns and we query across types, JOINED when they have many required columns of their own. The reason gets written down so later teams can tell whether it still holds.

I would also use check constraints so rules like a card payment needing its last four digits are enforced for every reader, not only in Java. Finally, new hierarchies go through design review, because once one has production data, removing it is a migration project.

For existing deep hierarchies I would not rewrite for purity; a flattening view for reporting is often enough until a real project justifies a migration.

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 "Object-relational mapping encourages modelling the database around class hierarchies....": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/c545fdb538/