Java Reference
In-Depth Information
Bidirectional one-to-one
In unidirectional relationships, navigating the domain model can be done only one way. In
listing 9.17 the BillingInfo domain object can be reached through the User object,
but you can't go the other way—you can't reach the User object through BillingInfo .
In bidirectional relationships, JPA puts domain model references together to enable you to
go both ways. Bidirectional one-to-one relationships are implemented using @OneToOne
annotations pointing to each other on both sides of the relationship. Let's see how this
works in the next listing by updating the code from listing 9.17 .
Listing 9.18. Bidirectional one-to-one relationship
In this listing, the User class has a relationship to the BillingInfo class through the
billingInfo property . This is no different than the unidirectional one-to-one rela-
tionship of listing 9.17 . But in this example the relationship is bidirectional because the
BillingInfo class also has a reference to the User class through the user property
. The @OneToOne annotation on the user property uses mappedBy and optional .
The mappedBy="billingInfo" configuration tells JPA that the “owning” side of the
relationship is the User class's billingInfo property. The optional= "false"
configuration tells JPA that BillingInfo can't exist without a User . This is interesting
because the @OneToOne annotation in User doesn't have this configuration. This means
Search WWH ::

Custom Search