Java Reference
In-Depth Information
Although the CategoryId class of listing 9.9 isn't very different from the Cat-
egoryKey class of listing 9.8 , changes to the Category class will be very different.
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
Search WWH ::




Custom Search