Hibernate: Inheritance - SINGLE_TABLE, JOINED and TABLE_PER_CLASS

Single table is fastest but forces every subclass column to be nullable; joined is normalised but needs a join per query.

Code
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "payment_type")
abstract class Payment { @Id Long id; BigDecimal amount; }

@Entity @DiscriminatorValue("CARD")
class CardPayment extends Payment { String last4; }

@Entity @DiscriminatorValue("UPI")
class UpiPayment extends Payment { String vpa; }
Output
SINGLE_TABLE: payments(id, amount, payment_type, last4, vpa)
last4 and vpa must both be nullable - the price of one table.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11