Java Reference
In-Depth Information
10.1.4. Using EntityManager in ActionBazaar
You'll explore the EJB 3 EntityManager interface by implementing an ActionBazaar
component. You'll implement the ItemManagerBean stateless session bean used to
provide the operations to manipulate items. As listing 10.1 demonstrates, the session
bean provides methods for adding, updating, and removing Item entities using the JPA
EntityManager . This is a good bean on which to focus because items are at the heart of
the ActionBazaar application. Users list items for auction and bid on items, and orders are
generated for winning bids.
Listing 10.1. ItemManager performs basic CRUD operations for items
Let's next look at how you acquire a reference to the EntityManager .
10.1.5. Injecting the EntityManager
The first step to performing any persistence operation is to acquire an instance of the
EntityManager . Acquiring a reference is easy; you annotate an EntityManager
member variable with @PersistenceContext , as shown in listing 10.1 . The container
takes care of the mundane tasks of looking up, opening, and closing the EntityManager
behind the scenes. In addition, unless specified, injected EntityManager s have transac-
tion scope by default. To better understand how injection is performed, let's take a look at
the annotation:
Search WWH ::

Custom Search