Sobes.tech
Senior

What inheritance strategies do you know in JPA? Which ones have you used?

sobes.tech AI

Answer from AI

In JPA, there are three main inheritance strategies for entities:

  1. SINGLE_TABLE — all subclass classes are stored in one table with an additional discriminator column to determine the type.

    • Pros: high performance, simple queries.
    • Cons: many nullable columns, possible redundancy.
  2. JOINED — each class has its own table, and tables are linked by a key.

    • Pros: normalized structure, less duplication.
    • Cons: complex join queries, may be slower.
  3. TABLE_PER_CLASS — each class has its own table with all fields, including inherited ones.

    • Pros: simplicity, independence of tables.
    • Cons: data duplication, complex join queries.

I often used the SINGLE_TABLE strategy for simple hierarchies where performance is important and there are not many unique fields in subclasses. JOINED was used when normalization and clear separation of data by types were needed.