Java Reference
In-Depth Information
private String zipCode;
}
The
User
entity then has a
Collection
of
Address
objects:
@Entity
@Table(name="USERS")
public class User {
@ElementCollection
@CollectionTable(name="HOMES")
private Set<Address> shippingAddresses;
}
When working with collections, database primary-key values become vital. It's through
primary-key values that databases form relations. JPA can then use these relations to pull
the correct data from other tables into collections for you. So next we're going to look at
how JPA is configured to identify primary keys.
9.3.7. Specifying entity identity
When we talk about specifying an entity identity, what we're really talking about is how
JPA identifies a database table's primary key. All database tables must have some way to
uniquely identify a row in the table, so that when data needs to be updated only the data in
that one row is changed and the rest of the data in the table remains unaffected. Likewise,
when JPA pulls data out of the database and converts it to your Java object domain model,
JPA needs to be able to uniquely identify the in-memory objects that hold the data from
rows in your table. All this is done by configuring JPA so that it knows about your table's
primary key. JPA has three ways of accomplishing this, and we'll look at how to use each
one of them:
• Using the
@Id
annotation
• Using the
@IdClass
annotation
• Using the
@EmbeddedId
annotation
@Id annotation
When the primary key of your database table is a single column, you want to use the
@Id
erty holds the value of the primary-key column of the
CATEGORY
table in the database.