Java Reference
In-Depth Information
String restaurantId) {
bb return entityManager.find(Restaurant.class,
bbbbbbbbbbbbbbbbbbbbbbbbbbb new Integer(restaurantId));
}
Finds the Restaurant
Executes named query
public List findAvailableRestaurants(
Address deliveryAddress, Date deliveryTime) {
bb Query query = entityManager
bb .createNamedQuery("Restaurant.findAvailableRestaurants");
bb Calendar c = Calendar.getInstance();
bb c.setTime(deliveryTime);
bb int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
bb int hour = c.get(Calendar.HOUR_OF_DAY);
bb int minute = c.get(Calendar.MINUTE);
bb String zipCode = deliveryAddress.getZip();
bb query.setParameter("dayOfWeek", new Integer(
bb dayOfWeek));
bb query.setParameter("hour", new Integer(hour));
bb query.setParameter("minute", new Integer(
bb minute));
bb query.setParameter("zipCode", new Integer(
bb zipCode));
bb return query.getResultList();
}
public boolean isRestaurantAvailable(
Address deliveryAddress, Date deliveryDate) {
bb return !findAvailableRestaurants(
bb deliveryAddress, deliveryDate)
bb .isEmpty();
}
}
In listing 10.2, the constructor takes the EntityManager as a parameter and stores
it in a field. The findRestaurant() method then calls EntityManager.find() to
retrieve the specified restaurant. Finally, the findAvailableRestaurants() method
uses the EntityManager to execute a named query that finds the available restaurants.
Note that as of the time of this writing, Spring does not yet support EJB 3 and so
this repository uses the EJB 3 API s directly rather than using a Spring ORM template
class. It also throws EJB exceptions instead of Spring data access exceptions. Once
Spring has an EJB 3 ORM template class, we will be able to simplify the code, which
will make it a little easier to test, and use Spring's exception mapping mechanism,
which will enable the application to handle data access exceptions uniformly.
 
 
Search WWH ::




Custom Search