liveBook · Manning

Polymorphic association

As explained in Java Persistence with Spring Data and Hibernate epub

A polymorphic association is an object association whose target may be an instance of any of several subtypes.

A polymorphic association targets a superclass while referring at runtime to an instance of any concrete subtype. For example, User.defaultBilling can reference either CreditCard or BankAccount. SQL foreign keys normally identify exactly one target table, so relational schemas need an ORM inheritance strategy to represent this relationship. section 1.2.2 section 7.8

A polymorphic many-to-one association works as follows:

  • Map the target superclass as an entity with inheritance, and declare the association against that superclass. section 7.8.1
  • Store the relationship in the owning table; User receives a nullable DEFAULTBILLING_ID join column because no default billing method may be assigned. section 7.8.1
  • Assign a concrete subtype such as CreditCard, persist both entities, and navigate the association later; Hibernate retrieves the concrete instance and dispatches its pay method. section 7.8.1
  • Apply the same idea to one-to-one associations; plural associations require mapping the corresponding collection relationship. section 7.8.1

Default billing details

A User declares `@ManyToOne private BillingDetails defaultBilling;`. A CreditCard is assigned to that property and saved with the User. Loading users later allows `users.get(0).getDefaultBilling().pay(123)` to invoke the concrete billing subtype.

Inheritance strategy affects how well polymorphic associations are supported. Table-per-concrete-class with implicit polymorphism is simple but weak for these associations; table-per-concrete-class with unions supports them conditionally. Table-per-class-hierarchy favors performance and simplicity, while joined subclass tables favor normalization and integrity at possible query cost. section 7.9

Figure 7.6 A user has either a credit card or a bank account as the default billing details.

Use the class diagram to see User.defaultBilling pointing to either CreditCard or BankAccount. section 7.8

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains superclass mapping, nullable join columns, persistence, retrieval, method dispatch, and inheritance-strategy tradeoffs.
Examples
Uses User, BillingDetails, CreditCard, BankAccount, DEFAULTBILLING_ID, and a subclass-specific pay call.
What it assumes
Assumes Java classes, subclassing, SQL tables, foreign keys, entities, and basic Spring Data JPA usage.
What you can do after
Build it from scratch by mapping an inherited target, persisting a concrete subtype, and navigating the association polymorphically.
How it connects
Connects polymorphism with inheritance mapping, ORM, Hibernate, JPA, foreign keys, and relational schema design.

Related concepts

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.