liveBook · Manning

Implicit polymorphism

As explained in Java Persistence with Spring Data and Hibernate epub

Implicit polymorphism is Hibernate behavior that supports polymorphic queries across mapped classes sharing a common interface or type without requiring a mapped inheritance hierarchy.

Implicit polymorphism lets Hibernate answer polymorphic queries without an explicit mapped inheritance hierarchy. It can query a persistent interface and combine results from every mapped class that implements it. section 7.7

A table-per-concrete-class mapping applies the behavior through several coordinated choices:

  • Map each concrete class with @Entity; each class receives an independent table containing its own and inherited columns. section 7.1
  • Annotate the shared superclass with @MappedSuperclass, because superclass properties are otherwise ignored and remain nonpersistent. section 7.1
  • Override inherited column mappings with @AttributeOverride when concrete tables require different names, such as CC_OWNER. section 7.1
  • Share repository behavior through a generic @NoRepositoryBean base interface while exposing concrete repositories for each entity type. section 7.1

Billing details and electronic payment options

CreditCard and BankAccount can extend BillingDetails while persisting to separate CREDITCARD and BANKACCOUNT tables. A shared BillingDetailsRepository<T extends BillingDetails, ID> supplies findByOwner, and concrete repositories add findByExpYear or findBySwift. Separately, Hibernate can execute select o from ElectronicPaymentOption o even when implementing classes come from different persistent hierarchies. section 7.1 section 7.7

Figure 7.1 Mapping all concrete classes to an independent table

Use the mapping diagram to compare the independent-table layout for concrete classes. section 7.1

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Details concrete-table mappings, superclass persistence, column overrides, repositories, and query resolution across mixed persistent classes.
Examples
Uses BillingDetails, CreditCard, BankAccount, independent tables, generic repositories, and an ElectronicPaymentOption interface query.
What it assumes
Assumes JPA entity and repository concepts, while demonstrating mapped-superclass persistence and interface-based polymorphic querying.
What you can do after
Apply Hibernate mappings and repository interfaces to persist concrete subclasses and query implementing classes polymorphically.
How it connects
Connects mapped superclasses, concrete-table inheritance, Spring Data repositories, inheritance strategies, and polymorphic associations.

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.