Java Reference
In-Depth Information
The named
EJB
QL
query that is executed by the
EJB3RestaurantRepository
is
defined using the
@NamedQuery
annotation of the
Restaurant
entity bean:
@Entity(access = AccessType.FIELD)
@NamedQuery(name = "Restaurant.findAvailableRestaurants",
queryString = "SELECT OBJECT(restaurant) "
+ "FROM Restaurant as restaurant, "
+ " IN(restaurant.serviceArea) zip, "
+ " IN(restaurant.timeRanges) tr "
+ "WHERE zip.zipCode = :zipCode AND tr.dayOfWeek = :dayOfWeek "
+ " AND ( (tr.openHour < :hour "
+ " OR (tr.openHour = :hour AND tr.openMinute <= :minute))"
+ " AND (tr.closeHour > :hour "
+ " OR (tr.closeHour = :hour AND tr.closeMinute > :minute)
+ "))")
@Table(name = "FTGO_RESTAURANT")
public class Restaurant implements Serializable {
…
}
This query is pretty similar to the
HQL
and
JDOQL
queries you saw earlier in chap-
ters 5 and 6. It finds those restaurants whose
serviceArea
contains a
ZipCode
for
the specified for ZIP code and whose
timeRanges
field contains a
TimeRange
for
the specified day, hour, and minute. It is interesting to see that the long queries
defined in using the
@NamedQuery
annotation have the same readability problems
as queries defined in Java code. They must be split up into multiple strings that
are concatenated together. In comparison, it's much easier to write a long query
in an
XML
document.
Next let's consider the issue of testing entity beans.
10.2.3
Testing the persistent EJB domain model
Naturally, a discussion of
EJB 3
persistence would not be complete without a men-
tion of testing. Testing
EJB 2
entity beans was quite difficult because they had to be
deployed in the
EJB
container. What's worse, the tests also needed to be deployed
in the application server so that they could access the entity beans using their
local interface. As well as making testing more complicated, the deployment step
slowed down the edit-compile-debug cycle. In comparison, testing an
EJB 3
domain model is straightforward because
EJB 3
entity beans can run outside the
container. We can write and execute persistence tests for persistent objects and
repositories in the same way that did in a
JDO
or Hibernate application. They are
regular
JU
nit-based tests that are easily run from within the
IDE
.
Listing 10.3 shows an example of such a test. This test creates a
PendingOrder
entity bean and updates its delivery information. The test class has a
setup()
Search WWH ::
Custom Search