Java Reference
In-Depth Information
public static class AccountPk {
// ...
}
}
Regardless of which of these approaches we take to declare our compound primary key,
the table that will be used to represent it will require the same set of columns. Listing 6-9
shows the DDL that will be generated from any of Listings 6-6, 6-7, or 6-8.
Listing 6-9.
The DDL Generated from the Annotated
Account
Class (Regardless of the
Approach Used)
create table Account (
code varchar(255) not null,
number integer not null,
description varchar(255),
primary key (code, number)
);
Database Table Mapping with @Table and @SecondaryTable
The
@Table
annotation allows you to specify the details of the table that will be used to per-
sist the entity in the database. If you omit the annotation, Hibernate will default to using
the class name for the table name, so you only need to provide this annotation if you want
to override that behavior. The
@Table
annotation provides four attributes, allowing you to
override the name of the table, its catalog, and its schema, and enforce unique constraints
on columns in the table. Typically, you would only provide a substitute table name thus:
@Table(name=" ORDER_HISTORY")
. The unique constraints will be applied if the database
schema is generated from the annotated classes, and will supplement any column-specific
constraints (see discussions of
@Column
and
@JoinColumn
later in this chapter). They are not
otherwise enforced.
The
@SecondaryTable
annotation provides a way to model an entity bean that is persisted
across several different database tables. Here, in addition to providing an
@Table
annotation for
the primary database table, your entity bean can have an
@SecondaryTable
annotation, or an
@SecondaryTables
annotation in turn containing zero or more
@SecondaryTable
annotations.
The
@SecondaryTable
annotation takes the same basic attributes as the
@Table
annotation, with
the addition of the
join
attribute. The
join
attribute defines the join column for the primary
database table. It accepts an array of
javax.persistence.PrimaryKeyJoinColumn
objects. If you
omit the
join
attribute, then it will be assumed that the tables are joined on identically named
primary key columns.
When an attribute in the entity is drawn from the secondary table, it must be marked with
the
@Column
annotation, with a table attribute identifying the appropriate table. Listing 6-10
shows how a property of the
Customer
entity could be drawn from a second table mapped in
this way.
Search WWH ::
Custom Search