Java Reference
In-Depth Information
Listing 9.16. Code-generated strategy
In this listing you see a few changes to the User class. First, the @GeneratedValue
annotation is no longer being used. This is because the primary key isn't being auto-
matically generated anymore; you're instead generating the key yourself. Second, the
@PrePersist annotation is introduced, which tells JPA to execute the gener-
atePrimaryKey() method before data is inserted into the database. And third, the JDK
UUID object is used as a simple example of a primary-key generation strategy
We've finished covering the basics of getting data into and out of individual database tables
using JPA. Next, we'll look at the “relation” part of relational databases and see how JPA
handles building object trees from related database data across multiple tables.
9.4. Entity relationships
Referring back to figure 9.3 , the ActionBazaar domain model consists of a number of dif-
ferent entities that have relationships among them. Up until now, we've been concentrating
on how JPA stores and retrieves data for a single entity. But now we're going to look at
how JPA handles the relationships between entities. A relationship essentially means that
one entity holds an object reference to another. For example, the Bid object holds a refer-
ence 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 uni- or bidirection-
al. The relationship between Bidder and Bid in figure 9.3 is unidirectional, because the
Bidder object has a reference to Bid but the Bid object has no reference to the Bid-
der . The Bid-Item relationship, on the other hand, is bidirectional, meaning both the
Search WWH ::

Custom Search