Quick revision
The short answer
I would choose SINGLE_TABLE, because the dominant query lists payments of all types for an account, and one indexed table answers it without joins. The 3 to 8 type-specific columns are nullable, so integrity comes from check constraints per discriminator value. JOINED would be cleaner if the type-specific data were large or rarely read, at the cost of joins; a JSON column for type-specific attributes is an option when the schema changes often; and it is worth asking whether composition, a payment with a method-details component, is clearer than inheritance at all.
What to remember
In this answer, we chose SINGLE_TABLE for card, bank transfer and wallet payments because the dominant query lists all types for an account and one indexed table answers it without joins. Check constraints per discriminator value restore the integrity that nullable subclass columns lose. JOINED would suit large or rarely read type data, a JSON column suits frequently changing attributes, and composition with a method-details component is worth considering when the types differ in data rather than behaviour.
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.
I would choose SINGLE_TABLE, because the dominant query lists payments of all types for an account, and one indexed table answers it without joins. The 3 to 8 type-specific columns are nullable, so integrity comes from check constraints per discriminator value. JOINED would be cleaner if the type-specific data were large or rarely read, at the cost of joins; a JSON column for type-specific attributes is an option when the schema changes often; and it is worth asking whether composition, a payment with a method-details component, is clearer than inheritance at all.
Why this question is asked
Payments are a good test case for inheritance mapping because the numbers are concrete: three types, five shared fields, a handful of specific fields each, and one query that dominates. Interviewers want to see the decision driven by those numbers and the query pattern, with a plan for the weaknesses of the chosen option. In this answer, we will recap the requirements, map them with SINGLE_TABLE and see the SQL for the main query, protect integrity with check constraints, consider JOINED and a JSON column as alternatives, and look at composition instead of inheritance.
1A Quick Recap of the Requirements
We have card payments, bank transfers and wallet payments. All share five fields: id, accountId, amount, currency and createdAt (plus perhaps status). Each type has 3 to 8 specific fields: a card has last four digits, scheme and authorization code; a transfer has an IBAN, a BIC and a reference; a wallet has a provider and a wallet transaction id. In total that is around 20 type-specific columns. Most queries are the account's payment history: all types, newest first, one page at a time.
That last fact is the most important one. An inheritance strategy is a choice about which queries are cheap, so we design for the query that runs most.
2Mapping With SINGLE_TABLE
All three classes live in one payment table with a discriminator column. The main query becomes a single index range scan.
| Strategy | SQL for an account's history | Cost |
|---|---|---|
SINGLE_TABLE | one table, index on (account_id, created_at) | one index scan |
| JOINED | root + 3 outer joins | 4 tables per page |
TABLE_PER_CLASS | UNION ALL of 3 tables, then sort | 3 scans + sort |
| JSON details column | one table, details in one column | one index scan |
SINGLE_TABLE and the JSON variant are the cheap options.Notice there are no joins and no unions: Hibernate reads the method column of each row and builds the right subclass. With about 25 columns the table is wide but nowhere near any database limit, and null columns cost very little storage (PostgreSQL keeps a null bitmap per row).
3Protecting Integrity With Check Constraints
The weakness of SINGLE_TABLE is that subclass columns must be nullable, since a bank transfer has no card number. Without help, the database would accept a CARD row with no last four digits. We restore the rules with check constraints keyed on the discriminator.
Read each constraint as if the type is CARD, then these columns are required. The third kind forbids fields that do not belong to the type, which catches mapping bugs. Bean Validation annotations on the subclasses add the same rules in Java for earlier, friendlier errors, but the constraints are the guarantee.
4When JOINED or a JSON Column Fits Better
JOINED would be the better choice if type-specific data were large or rarely needed with the list, for example if card payments carried a big 3-D Secure authentication record. Keeping that in card_payment keeps the payment table narrow and fast for history pages, and the extra join is paid only when someone opens a card payment's details. With our 3 to 8 small fields, the joins on every page would cost more than they save.
A JSON column is the option when the type-specific attributes change often, for instance if new wallet providers each bring their own fields. The shared five fields stay as real, indexed columns, and the rest goes into a details column mapped with @JdbcTypeCode(SqlTypes.JSON) in Hibernate 6 and later. New attributes then need no migration. The price is weaker constraints (validate the JSON in the application, or use the database's JSON checks), and querying by a type-specific attribute needs JSON operators or an expression index.
5Do We Need Inheritance at All?
It is worth asking what the subclasses do. If the types differ only in their data, and the behaviour is the same (capture, refund, show in history), composition may be clearer: a single Payment entity with a method enum and an embedded or associated details object for each method. That avoids polymorphic proxies, which can make instanceof checks on lazy references misleading, and keeps the code focused on what payments have in common. If the types have real behaviour of their own, such as different refund rules, inheritance with SINGLE_TABLE expresses that well. Either way, the table can look almost the same; the choice is about the Java model.
6Follow-up questions
How would you add a fourth payment type later?
With SINGLE_TABLE, add nullable columns, a new discriminator value, a new subclass and its check constraint. Existing rows are untouched. With the JSON variant, add a new details type and its validation, with no schema change at all.
Should the discriminator be a string or an integer?
A short string such as CARD is readable in queries and logs and costs little. Integers save a few bytes but make every ad hoc query harder to read. Either way, keep the values stable, because they are stored in every row.
What about sensitive card data in this table?
Store only what PCI DSS allows, such as the last four digits and a token from the payment provider, never the full card number or security code. That rule applies regardless of the mapping strategy.
7Conclusion
In this answer, we chose SINGLE_TABLE for card, bank transfer and wallet payments because the dominant query lists all types for an account and one indexed table answers it without joins. Check constraints per discriminator value restore the integrity that nullable subclass columns lose. JOINED would suit large or rarely read type data, a JSON column suits frequently changing attributes, and composition with a method-details component is worth considering when the types differ in data rather than behaviour.
Open with
Since most queries list all payment types for an account, I'd go with SINGLE_TABLE. Everything is in one payment table with a discriminator column, so the account history is one index scan on account_id and created_at, no joins and no unions, and Hibernate builds the right subclass from the discriminator.
Then, if they want more
With five shared fields and three to eight specific fields per type, we're looking at maybe twenty-five columns, which is fine.
The known weakness is that subclass columns must be nullable, so I'd add check constraints per discriminator value, for example if method is CARD then card_last4 and scheme must be present, and also forbid card fields on non-card rows, with Bean Validation in Java for nicer errors.
I'd consider JOINED if type-specific data were large or rarely read, because it keeps the main table narrow, but here it would add three joins to every history page.
Another option is keeping the shared fields as columns and putting the type-specific attributes into a JSON column, which is nice if new wallet providers keep adding fields, at the cost of weaker constraints.
And I'd ask whether we need inheritance at all: if the types differ only in data, a single Payment with a method enum and a details component can be clearer.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6
- 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 "You must model payments that can be card, bank transfer or wallet, with 5 shared fields...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/52019439f3/