background image

Entity Mapped to Multiple Database Tables

<< Compound Primary Keys | Cascade Operations in the order Application >>
<< Compound Primary Keys | Cascade Operations in the order Application >>

Entity Mapped to Multiple Database Tables

@IdClass(order.entity.PartKey.class)
@Entity
...
public class Part {
...
@Id
public String getPartNumber() {
return partNumber;
}
...
@Id
public int getRevision() {
return revision;
}
...
}
Entity Mapped to More Than One Database Table
Part
's fields map to more than one database table: EJB_ORDER_PART and
EJB_ORDER_PART_DETAIL
. The EJB_ORDER_PART_DETAIL table holds the specification and
schematics for the part. The @SecondaryTable annotation is used to specify the secondary table.
...
@Entity
@Table(name=
"EJB_ORDER_PART")
@SecondaryTable(name=
"EJB_ORDER_PART_DETAIL", pkJoinColumns={
@PrimaryKeyJoinColumn(name=
"PARTNUMBER",
referencedColumnName=
"PARTNUMBER"),
@PrimaryKeyJoinColumn(name=
"REVISION",
referencedColumnName=
"REVISION")
})
public class Part {
...
}
EJB_ORDER_PART_DETAIL
shares the same primary key values as EJB_ORDER_PART. The
pkJoinColumns
element of @SecondaryTable is used to specify that EJB_ORDER_PART_DETAIL's
primary key columns are foreign keys to EJB_ORDER_PART. The @PrimaryKeyJoinColumn
annotation sets the primary key column names and specifies which column in the primary table
the column refers to. In this case, the primary key column names for both
EJB_ORDER_PART_DETAIL
and EJB_ORDER_PART are the same: PARTNUMBER and REVISION,
respectively.
The order Application
Chapter 26 · Persistence in the EJB Tier
717