Java Reference
In-Depth Information
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. We'll discuss each of these relationships and their corres-
ponding annotations in the next sections.
9.4.1. One-to-one relationships
The
@OneToOne
annotation is used to mark uni- and bidirectional one-to-one relation-
ships. The ActionBazaar example in
figure 9.3
has no one-to-one relationship. But you can
imagine that the
User
domain object has a one-to-one relationship with a
BillingInfo
object. The
BillingInfo
object might contain billing data on a user's credit card, bank
account, and so on. Let's start by seeing what a unidirectional relationship would look like.
Unidirectional one-to-one
Let's assume the
User
object has a reference to the
BillingInfo
but not vice versa. In
other words, the relationship is unidirectional, as shown in
figure 9.6
.
Figure 9.6. A one-to-one unidirectional relationship between
User
and
BillingInfo
The following listing illustrates this relationship.