Java Reference
In-Depth Information
Both of the business processes persist data. The
BidService
needs to add a bid record to
the database. Similarly, the
OrderProcessor
must add an order record. These database
changes are performed through two entities in the JPA-managed persistence tier—the
Bid
and
Order
entities. Whereas the
BidService
uses the
Bid
entity, the
OrderPro-
cessor
uses the
Order
entity. The business-tier components use
BidDao
and
Order-
Dao
persistence-tier components. Note that the DAOs need not be EJBs because there's
no need to use EJB services directly at the persistence layer. Indeed, as you'll soon see,
the DAOs are minimalistic Java objects managed by CDI that use no services other than
dependency injection. Recall that although JPA 2 entities contain ORM configuration, they
don't persist themselves. As you'll see in the actual code, the DAOs have to use the JPA 2
EntityManager
API to add, delete, update, and retrieve entities.
If your mental picture matches up with
figure 2.2
pretty closely, it's likely the code we're
going to present next will seem intuitive too, even though you don't know EJB 3.
2.2. Building business logic with EJB 3
Let's start exploring the solution from the business logic tier, just like you would in a real-
world application. EJB 3 session beans are ideal for modeling the bidding and ordering
processes in your scenario. By default they're transactional, thread-safe, and pooled—all
characteristics you need to build the business logic tier of an enterprise application. Session
beans are the easiest but most versatile part of EJB 3, so they're a great place to start.
Recall that session beans come in three flavors: stateful, stateless, and singleton. You'll use
stateless beans and stateful beans in the examples, but not singleton beans. You'll also save
message-driven beans for later. You'll take on stateless session beans first, primarily be-
cause they're simpler.
2.2.1. Using stateless session beans
Stateless session beans are used to model actions or processes that can be done in a single
method call, such as placing a bid on an item in your ActionBazaar scenario. A vast major-
ity of your business-tier components are likely to be stateless. The
addBid
bean method
in
listing 2.1
is called from the ActionBazaar web tier when a user decides to place a bid.
The parameter to the method, the
Bid
object, represents the bid to be placed. The
Bid
ob-