Java Reference
In-Depth Information
The DAO is very simple. Some of you might be surprised that neither the DAO class nor
the interface has any annotations on it at all. This is normal in the CDI world because
CDI-managed beans truly are bare POJOs. When CDI sees the @Inject annotation in the
BidService EJB, it looks for any object that implements the BidDao interface and in-
jects it. It's important to note that the BidDao is “nameless”—it doesn't have an @Named
annotation. This is because the DAO doesn't need to be referenced by name anywhere. The
DAO also doesn't have a defined scope. When this happens, CDI assumes that the bean
belongs in the default scope. By default, CDI creates a brandnew instance of the bean and
injects it into the dependent component—basically the same behavior as a “new” operator
at the injection point. Notice also that the BidDAO itself can request injection. In the ex-
ample, CDI injects a JPA 2 entity manager into the DAO that's used to save the Bid entity
into the database. We'll talk more about JPA 2 in the next section.
The CDI features you saw in this section are truly the tip of the iceberg. CDI has a vast
array of other features like stereotypes, events, interceptors, and decorators, some of which
we'll look at in chapter 12 . For now, let's turn our attention to the last piece of the EJB 3
puzzle: JPA 2.
2.4. Using JPA 2 with EJB 3
JPA 2 is the de facto persistence solution for the Java EE platform and is a closely related
API to EJB 3. In Java EE 5, JPA was part of EJB 3. If you're familiar with Hibernate,
TopLink, or JDO, you'll be right at home with JPA 2. Most of these tools now strongly
support the JPA 2 standard.
As you'll see shortly, the JPA EntityManager interface defines the API for persistence
operations, whereas JPA entities specify how application data is mapped to a relational
database. Although JPA takes a serious bite out of the complexity of saving enterprise data,
O/R mapping-based persistence is still a nontrivial topic. We'll devote chapters 9 , 10 , and
11 of this topic to JPA.
In almost every step of the ActionBazaar scenario, data is saved into the database using
JPA 2. It's neither necessary nor very interesting to go over every one of those scenarios at
the moment. Instead, you'll see what JPA 2 looks like by revisiting the BidDAO . As a brief
visual reminder, figure 2.5 depicts the various components that interact with one another
when a bidder creates a bid in ActionBazaar. We'll first take a look at how the Bid entity
Search WWH ::




Custom Search