Java Reference
In-Depth Information
Listing 2.9.
EntityManager
operations in the
BidDao
The true magic of the code in this listing lies in the
EntityManager
interface. One in-
teresting way to think about the
EntityManager
interface is as an interpreter between
the object-oriented and relational worlds. The manager reads the O/R mapping annotations
like
@Table
and
@Column
on the
Bid
entity and figures out how to save the entity into
the database. The
EntityManager
is injected into the
DefaultBidDao
bean through
the
@PersistenceContext
annotation
.
In the
addBid
method, the
EntityManager persist
method is called to save the
Bid
data into the database . After the
persist
method returns, an SQL statement
much like the following is issued against the database to insert a record corresponding to
the bid:
INSERT INTO BIDS (BID_ID, BIDDER_ID, BID_AMOUNT, ITEM_ID)
VALUES (52, 60, 200.50, 100)
It might be instructive to look back at
listing 2.7
now to see how the
EntityManager
figures out the SQL to generate by looking at the O/R mapping annotations on the
Bid