September 2010
Intermediate to advanced
766 pages
18h 35m
English
In the table per concrete class strategy, a database table is defined for each concrete class in the hierarchy. Each table has columns representing its properties and all properties of any superclasses:
CREATE TABLE "PUBLIC"."TABLEPERCLASS_PERSON" ( ID bigint PRIMARY KEY NOT NULL, FIRSTNAME varchar, LASTNAME varchar ) ; CREATE TABLE "PUBLIC"."TABLEPERCLASS_CUSTOMER" ( ID bigint PRIMARY KEY NOT NULL, FIRSTNAME varchar, LASTNAME varchar, CITY varchar, STATE varchar, STREET varchar, ZIP varchar ) ; CREATE TABLE "PUBLIC"."TABLEPERCLASS_EMPLOYEE" ( ID bigint PRIMARY KEY NOT NULL, FIRSTNAME varchar, LASTNAME varchar, CITY varchar, STATE varchar, STREET varchar, ZIP varchar, EMPLOYEEID integer ) ;
One major difference between this strategy and the SINGLE_TABLE strategy is that no discriminator
column is needed in the database schema. Also notice that each table
contains every persistent property in the hierarchy. Let’s now look at how
we map this strategy with annotations:
@Entity(name = "TABLEPERCLASS_PERSON") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class Person { @Id @GeneratedValue(strategy = GenerationType.TABLE) // Cannot accept default generation strategy for table-per-class private Long id; private String firstName; private String lastName; ... } @Entity(name = "TABLEPERCLASS_CUSTOMER") public class Customer extends Person { private String street; private String city; private String state; private String zip; ... } @Entity(name = "TABLEPERCLASS_EMPLOYEE") ...