liveBook · Manning

Union subclass mapping

As explained in Java Persistence with Spring Data and Hibernate epub

A union-based mapping represents polymorphic access to concrete-class tables by combining their rows with SQL UNION operations.

Union subclass mapping stores each concrete subclass in its own table while duplicating superclass columns. In JPA, declare InheritanceType.TABLE_PER_CLASS on the abstract superclass; each subclass remains an entity, and the superclass must define the shared identifier. section 7.2

BillingDetails mapped with TABLE_PER_CLASS

An abstract BillingDetails entity can define the generated ID and owner fields, while CreditCard and BankAccount inherit those properties and persist to CREDITCARD and BANKACCOUNT. A repository can expose findByOwner and findAll operations for the hierarchy. section 7.2

Polymorphic query SQL

A query such as select bd from BillingDetails bd becomes a derived table assembled with UNION ALL. Each branch selects the columns for one concrete table, supplies nulls for fields belonging to the other subtype, and adds a discriminator literal such as 1 or 2. section 7.2

Listing 7.8 Mapping BillingDetails with TABLE_PER_CLASS
Path: Ch07/mapping-inheritance-tableperclass/src/main/java/com/manning
 /javapersistence/ch07/model/BillingDetails.java
 
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BillingDetails {
    @Id
    @GeneratedValue(generator = “ID_GENERATOR”)
    private Long id;
    @NotNull
    private String owner;
    //  . . .
}

Use the superclass mapping listing to inspect the TABLE_PER_CLASS annotation, generated identifier, and shared owner property. section 7.2

The relational schema has no inheritance relationship between the tables; they merely repeat compatible columns. Choose this strategy when polymorphic access is absent or uncommon, or weigh it against SINGLE_TABLE and JOINED using hierarchy width, subtype attributes, and real SQL execution plans. section 7.2 section 7.7

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains annotations, identifiers, repositories, schema shape, and generated UNION SQL, but defers detailed performance analysis to real execution plans.
Examples
Uses BillingDetails, CreditCard, BankAccount, Spring Data repositories, JPQL, and generated UNION ALL SQL.
What it assumes
Assumes familiarity with entities, inheritance, repositories, JPQL, and relational tables; explicitly requires a mapped superclass identifier.
What you can do after
Operate/configure a TABLE_PER_CLASS hierarchy and evaluate whether unions suit its polymorphic access patterns.
How it connects
Connects inheritance mapping with polymorphic queries, polymorphic associations, SINGLE_TABLE, and JOINED strategies.

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.