liveBook · Manning

Mapped superclass

As explained in Java Persistence with Spring Data and Hibernate epub

@MappedSuperclass is a Jakarta Persistence annotation that marks a class whose persistent properties are inherited by mapped subclasses without making the superclass itself an entity.

@MappedSuperclass marks a class whose persistent properties are inherited by mapped subclasses. The superclass is not mapped to its own table; each concrete entity stores inherited properties in its table. Without the annotation, superclass properties are ignored by default. section 7.1

Use a mapped superclass when a hierarchy shares state but does not require a persistent superclass entity.

  • Declare shared identifiers and fields, such as id and owner, in the superclass; subclasses inherit their persistence mappings. section 7.1
  • Map concrete subclasses with @Entity, producing independent tables that contain both local and inherited columns. section 7.1
  • Override inherited column mappings with @AttributeOverride; JPA 2.2 permits repeated annotations, while earlier versions require @AttributeOverrides. section 7.1
  • Reuse repository behavior through a generic base Spring Data JPA repository marked @NoRepositoryBean, then specialize repositories for each entity. section 7.1
  • Avoid it when a polymorphic association or collection must target the superclass; that hierarchy must use @Entity and @Inheritance instead. section 7.8.2

Billing details hierarchy

Annotate abstract BillingDetails with @MappedSuperclass and place its generated id and owner there. CreditCard and BankAccount become @Entity classes with their own columns and tables; CreditCard can rename owner to CC_OWNER. A shared BillingDetailsRepository<T extends BillingDetails, ID> supplies findByOwner while concrete repositories add subtype-specific queries.

Mapped superclass for embeddables

Hibernate also permits an embeddable class to inherit persistent properties from a mapped superclass. Dimensions and Weight extend Measurement, inherit name and symbol, and override their column names so an Item can embed both without conflicts. An abstract Measurement property cannot be embedded polymorphically because no discriminator identifies the concrete subtype. section 7.6

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains table layout, column overrides, identifiers, repositories, embeddable inheritance, and polymorphic-association limits.
Examples
Uses BillingDetails, CreditCard, BankAccount, Measurement, Dimensions, Weight, Item, tables, and subtype repositories.
What it assumes
Assumes Java inheritance and persistence annotations, while demonstrating how @MappedSuperclass changes inherited-property persistence.
What you can do after
Build it from scratch by mapping shared fields, overriding inherited columns, and choosing repositories or entity inheritance for associations.
How it connects
Connects mapped superclasses with entity inheritance, implicit polymorphism, attribute overrides, embeddable classes, and Spring Data JPA repositories.

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.