Java Reference
In-Depth Information
HibernateRestaurantRepositoryImpl
extends
HibernateDaoSupport
, which pro-
vides convenience methods such as
setHibernateTemplate()
and
getHibernate-
Template()
. Let's look at the details:
Its constructor takes a
HibernateTemplate
as a parameter and calls
setHibernate-
Template()
, which is defined by its superclass.
The
findAvailableRestaurants()
method calls
makeParameterValues()
to create
the array of parameters. The
makeParameterValues()
uses a
Calendar
to extract
the components of the delivery time and returns them in an array along with the
delivery ZIP code.
B
C
D
It executes the named query by calling
HibernateTemplate.findByNamedQueryAnd-
NamedParam()
.
Once we write this method, the test we created earlier compiles and passes.
Because the test uses mock objects, it is not calling the real Hibernate
API
s to exe-
cute a query. To complete the implementation, we must write the query.
6.4.3
Writing the query that finds the restaurants
The
HibernateRestaurantRepositoryImpl
retrieves the available restaurants by
executing a named Hibernate query, which is stored in the Hibernate mapping
document. The query finds all restaurants whose service area contains the speci-
fied ZIP code and that have a
TimeRange
that matches the specified time:
<hibernate-mapping>
…
<query name="findAvailableRestaurants">
<![CDATA[
select r
from Restaurant r
inner join r.openingHours.timeRanges tr
where :zipCode in elements(r.serviceArea) 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)))
]]></query>
…
</hibernate-mapping>
Search WWH ::
Custom Search