Java Reference
In-Depth Information
method directly, HibernatePlaceOrderFacadeResultFactory calls HibernateTem-
plate.initialize() , which is a convenience method that calls Hibernate.ini-
tialize() and converts HibernateException to a Spring data access exception.
Using the HibernateTemplate simplifies the code and makes testing a lot easier
because it can be mocked. In contrast, Hibernate.initialize() is a static method
and impossible to mock.
The HibernatePlaceOrderFacadeResultFactory class extends the Spring class
HibernateDaoSupport and implements the PlaceOrderFacadeResultFactory inter-
face:
public class HibernatePlaceOrderFacadeResultFactory extends
HibernateDaoSupport implements
PlaceOrderFacadeResultFactory {
public HibernatePlaceOrderFacadeResultFactory(
HibernateTemplate hibernateTemplate) {
setHibernateTemplate(hibernateTemplate);
}
Saves HibernateTemplate
public PlaceOrderFacadeResult make(int statusCode,
PendingOrder pendingOrder) {
getHibernateTemplate().
initialize(pendingOrder.
bbbbbbbbbbbbbbb b
Initializes
line items
getLineItems());
Restaurant restaurant =
pendingOrder.getRestaurant();
if (restaurant != null) {
List menuItems =
restaurant.getMenuItems();
getHibernateTemplate().
bbbbbbbbbb b
Initializes optional
menu items
initialize(menuItems);
}
return new PlaceOrderFacadeResult(statusCode, pendingOrder);
}
HibernatePlaceOrderFacadeResultFactory defines a constructor that takes a
HibernateTemplate as a parameter and saves it for later. The make() method ini-
tializes the pending order's line items and the menu items for its restaurant (if it
has one). Unlike its JDO equivalent, HibernatePlaceOrderFacadeResultFactory
needs to have knowledge of the object structure. This isn't a problem in this
example since the object structure is so simple, but a factory that detaches a com-
plex object graph could be quite messy because, as we saw earlier, it would need to
contain conditional logic to handle null references and polymorphic references.
Search WWH ::




Custom Search