Java Reference
In-Depth Information
When using
@ClassId
, the
Category
class retained the individual properties compos-
ing the
CATEGORY
table's primary key. This completely changes when using
@Embed-
dedId
because those individual properties are now removed, replaced by the
Cat-
egoryId
class, and
Category
becomes a composite. The next listing shows these
changes.
Listing 9.10. Category using
@EmbeddedId
Let's look at this more closely. First, you'll notice that the
name
and
createDate
prop-
erties no longer exist. They've been replaced by the
categoryId
property , and
the
categoryId
property is now annotated with
@EmbeddedId
. This tells JPA
that
Category
is now a composite object and it should create an instance of the
Cat-
egoryId
class to hold the
CATEGORY
table's primary key. Getter methods on
Cat-
egory
are updated to use this embedded object
.
@Embeddable annotation
We introduced the
@Embeddable
annotation when discussing multicolumn primary keys
and how JPA uses both
@EmbeddedId
and
@Embeddable
together as a solution for the
primary-key mapping. It's important to note that the
@Embeddable
annotation isn't ex-
clusive to primary keys. Any object may be annotated with
@Embeddable
and be used
inside your Java domain model. An address is a good example. Multiple Java domain