background image

Table per Concrete Class Strategy

<< Single Table per Class Hierarchy Strategy | Managing Entities >>
<< Single Table per Class Hierarchy Strategy | Managing Entities >>

Table per Concrete Class Strategy

The javax.persistence.DiscriminatorValue annotation may be used to set the value
entered into the discriminator column for each entity in a class hierarchy. You may only
decorate concrete entity classes with @DiscriminatorValue.
If @DiscriminatorValue is not specified on an entity in a class hierarchy that uses a
discriminator column, the Persistence provider will provide a default, implementation-specific
value. If the discriminatorType element of @DiscriminatorColumn is
DiscriminatorType.STRING
, the default value is the name of the entity.
This strategy provides good support for polymorphic relationships between entities and queries
that cover the entire entity class hierarchy. However, it requires the columns that contain the
state of subclasses to be nullable.
The Table per Concrete Class Strategy
In this strategy, which corresponds to InheritanceType.TABLE_PER_CLASS, each concrete class
is mapped to a separate table in the database. All fields or properties in the class, including
inherited fields or properties, are mapped to columns in the class's table in the database.
This strategy provides poor support for polymorphic relationships, and usually requires either
SQL UNION queries or separate SQL queries for each subclass for queries that cover the entire
entity class hierarchy.
Support for this strategy is optional, and may not be supported by all Java Persistence API
providers. The default Java Persistence API provider in the Application Server does not support
this strategy.
The Joined Subclass Strategy
In this strategy, which corresponds to InheritanceType.JOINED, the root of the class hierarchy
is represented by a single table, and each subclass has a separate table that only contains those
fields specific to that subclass. That is, the subclass table does not contain columns for inherited
fields or properties. The subclass table also has a column or columns that represent its primary
key, which is a foreign key to the primary key of the superclass table.
This strategy provides good support for polymorphic relationships, but requires one or more
join operations to be performed when instantiating entity subclasses. This may result in poor
performance for extensive class hierarchies. Similarly, queries that cover the entire class
hierarchy require join operations between the subclass tables, resulting in decreased
performance.
Some Java Persistence API providers, including the default provider in the Application Server,
require a discriminator column in the table that corresponds to the root entity when using the
joined subclass strategy. If you are not using automatic table creation in your application, make
sure the database table is set up correctly for the discriminator column defaults, or use the
@DiscriminatorColumn
annotation to match your database schema. For information on
discriminator columns, see
"The Single Table per Class Hierarchy Strategy" on page 694
.
Entities
Chapter 24 · Introduction to the Java Persistence API
695