September 2010
Intermediate to advanced
766 pages
18h 35m
English
In the table per subclass mapping, each subclass has its own table,
but this table contains only the properties that are defined on that
particular class. In other words, it is similar to the TABLE_PER_CLASS strategy, except the schema is
normalized. This is also called the JOINED strategy:
CREATE TABLE "PUBLIC"."JOINED_PERSON" ( ID bigint PRIMARY KEY NOT NULL, FIRSTNAME varchar, LASTNAME varchar ) ; CREATE TABLE "PUBLIC"."JOINED_CUSTOMER" ( CITY varchar, STATE varchar, STREET varchar, ZIP varchar, ID bigint PRIMARY KEY NOT NULL ) ; ALTER TABLE "PUBLIC"."JOINED_CUSTOMER" ADD CONSTRAINT FK65AE08146E93989D FOREIGN KEY (ID) REFERENCES "PUBLIC"."JOINED_PERSON"(ID) ; CREATE TABLE "PUBLIC"."JOINED_EMPLOYEE" ( EMPLOYEEID integer, EMP_PK bigint PRIMARY KEY NOT NULL ) ; ALTER TABLE "PUBLIC"."JOINED_EMPLOYEE" ADD CONSTRAINT FK88AF6EE4D423ED9D FOREIGN KEY (EMP_PK) REFERENCES "PUBLIC"."JOINED_CUSTOMER"(EMP_PK) ;
When the persistence manager loads an entity that is a subclass or
traverses a polymorphic relationship, it does an SQL join on all the
tables in the hierarchy. In this mapping, there must be a column in each
table that can be used to join each table. In our example, the JOINED_EMPLOYEE, JOINED_CUSTOMER, and JOINED_PERSON tables share the same primary-key
values. The annotation mapping is quite simple:
@Entity(name="JOINED_PERSON") @Inheritance(strategy=InheritanceType.JOINED) public class Person { ... } @Entity(name="JOINED_CUSTOMER") public class Customer extends Person ...