Java Reference
In-Depth Information
Listing 9.7. Category with two column primary keys
You now have the
name
property and the
createdDate
property both annotated
with
@Id
, and this tells JPA that the two together uniquely identify the category. But what
JPA doesn't know yet is how to use these two properties when comparing two
Category
objects for equality. To do this, you introduce a new class,
CategoryKey
, as shown in
the next listing.
Listing 9.8.
CategoryKey
Let's take a look at
CategoryKey
in a little more detail. The JPA specification says
classes like this should implement
Serializable
, so the class does this and also
defines a
serialVersionUID
. The two fields,
name
and
createDate
, which
compose the primary key, are duplicated here in
CategoryKey
. A no-argument con-