Enterprise Java Development@TOPIC@

Java Persistence API: Inheritance

Mapping Entity Inheritance to the Database

Revision: v2013-10-05

Built on: 2014-03-07 00:07 EST

Abstract

This presentation provides information for mapping Java entity class inheritance to the database using JPA.


Purpose
1. Goals
2. Objectives
1. Inheritance Strategy: Single Table
1.1. Single Table Strategy Overview
1.2. Single Table Example Database Schema
1.3. Single Table Example Java Mapping
1.4. Single Table Example Usage
1.5. @DiscriminatorColumn Annotation
1.6. Summary
2. Inheritance Strategy:Table per Concrete Class
2.1. Table per Concrete Class Strategy Overview
2.2. Table per Concrete Class Example Database Schema
2.3. Table per Concrete Class Example Java Mapping
2.4. Table per Concrete Class Example Usage
2.5. Summary
3. Inheritance Strategy:Join
3.1. Join Strategy Overview
3.2. Join Example Database Schema
3.3. Join Example Java Mapping
3.4. Join Example Usage (Persist)
3.5. Summary
4. Inheritance Strategy:Non-Entity
4.1. Non-Entity Strategy Overview
4.2. Non-Entity Example Database Schema
4.3. Non-Entity Example Java Mapping
4.4. Non-Entity Example Usage (Persist)
4.5. Summary
5. Mixed Inheritance Strategies
5.1. Which Strategy Gets Used for Subclass?
5.2. Generated Database Schema
5.3. Summary

  • Parent defines default mapping for all derived types


  • Supplies type-specific column value


  • Accepts default type-specific column value


  • Two rows inserted into single base table with discriminator type supplied


  • Single table queried for objects


  • Single table "sparsely" filled based on type


  • Parent defines default mapping for all derived types, including PK generation

  • Using a common SEQUENCE allows sub-classes to have unique PKs across all tables


  • Subclasses name their specific entity class table


  • Rows for entities placed into separate tables


  • Query through parent type causes SQL "UNION ALL" of each concrete sub-class table


  • Table per concrete class

  • No unused columns

  • Parent columns repeated in each sub-class table


  • Each persist() must insert into concrete class table and parent class table(s)


  • Parent class table joined with each sub-class table during query of parent type


  • Entities span multiple tables


  • Parent class is not a legal entity -- has no @Id

Note

In this example, the implementation of BaseObject actually has an id attribute that the derived classes make use of. However, it is marked as @Transient in the base class and @Id in the derived Entity classes since MappedSuperClasses do not have primary keys. This specific example could have also used TABLE_PER_CLASS because of the availability of an id property in the base class.


  • "version" column name from parent being renamed

  • "id" property defined in this class given custom column name

  • Transient attribute in parent being reused to hold @Id for subclass using PROPERTY access


  • Entity accepts mapping defaults


  • Rows are inserted into type-specific entity class tables


  • Separate tables are accessed when obtaining each type


  • Separate tables per concrete class (like TABLE_PER_CLASS)

  • No unused columns (like TABLE_PER_CLASS)