Java Reference
In-Depth Information
missing the optional attribute. This is because an empty Set or List implicitly means
an optional relationship, meaning that the entity can exist even if no associations do.
We've now covered all of the relationship annotations in JPA. With these annotations, you
can configure your domain object relationships, whatever they may be. Table 9.3 provides
a quick summary of the annotations and their attributes.
Table 9.3. Summary of JPA entity relationship annotations
Attribute
@OneToOne
@OneToMany
@ManyToOne
@ManyToMany
targetEntity
Yes
Yes
Yes
Yes
cascade
Yes
Yes
Yes
Yes
fetch
Yes
Yes
Yes
Yes
optional
Yes
No
Yes
Yes
mappedBy
Yes
Yes
No
Yes
This concludes our summary of mapping entity relationships. The automatic mapping of
these relationships when retrieving data from the database is a powerful JPA feature. But as
Java developers, we have other OO techniques that we use in our domain model that JPA
also has to support. Building object hierarchies through inheritance is one of these features,
so next we'll look at how JPA maps object inheritance.
9.5. Mapping inheritance
So far, you've learned how to get data into and out of tables using JPA and how to define
relationships between entities. What we're going to look at next is a more detailed analysis
of how Java object inheritance affects how database tables are designed. The easiest way to
introduce this is with an example. Recall that in ActionBazaar there are two different kinds
of users: bidders and sellers. From a Java domain model point of view, you use inheritance
to keep properties common to both in a User object, and the Bidder and Seller ob-
jects have properties unique to those user types. Figure 9.9 shows this inheritance.
 
Search WWH ::




Custom Search