Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

7.3. Entity relationships

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.

Table 7.2. Domain relation types and corresponding annotations
Type of relationshipAnnotation
One-to-one@OneToOne
One-to-many@OneToMany
Many-to-one@ManyToOne
Many-to-many@ManyToMany



  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial