Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Earlier in the chapter, we explored the concepts of domain relationships, direction, and multiplicity. As a review, we’ll summarize those concepts here before diving into the details of how to specify domain relationships using JPA. As you might have noted in our domain object code samples, a relationship essentially means that one entity holds an object reference to another. For example, the Bid object holds a reference to the Item object the bid was placed on. Therefore, a relationship exists between the Bid and Item domain objects. Recall that relationships can be either unidirectional or bidirectional. The relationship between Bidder and Bid in figure 7.3 is unidirectional, since the Bidder object has a reference to Bid but the Bid object has no reference to the Bidder. The Bid-Item relationship, on the other hand, is bidirectional, meaning both the Bidder and Item objects have references to each other. Relationships can be one-to-one, one-to-many, many-to-one, or many-to-many. Each of these relationship types is expressed in JPA through an annotation. Table 7.2 lists the relationship annotations we’ll discuss in the following sections.
| Type of relationship | Annotation |
|---|---|
| One-to-one | @OneToOne |
| One-to-many | @OneToMany |
| Many-to-one | @ManyToOne |
| Many-to-many | @ManyToMany |