Java Reference
In-Depth Information
presentation tier, which will store it in the HttpSession or serialize it to the appli-
cation's client. When the user saves her changes, the presentation tier updates the
detached object with those changes and then passes it to the business tier. The
business tier then calls the persistence framework to reattach the object and
update the database.
Let's briefly review how to detach and attach JDO and Hibernate objects and
look at some of the problems with using them.
Detaching and attaching objects in JDO and Hibernate
As we have seen in chapter 7, the details of how you detach and attach objects
depend on whether you are using JDO or Hibernate. For example, the JDO ver-
sion of the business logic for the Acknowledge Order use case attaches and
detaches objects as follows. When it loads an Order at the start of the use case, it
calls JdoTemplate.detachCopy() to detach it:
Order order = orderRepository.findOrder(ordered);
Order detachedOrder = getJdoTemplate().detachCopy(order);
It then returns the detached order to the presentation tier, which stores it in the
HttpSession . After the user has confirmed that he wants to accept the order, the
presentation tier updates the detached order and calls the business logic to attach
it. The business tier calls attachCopy() :
Order order = getJdoTemplate().attachCopy(detachedOrder);
When the JDO implementation updates the database, which is typically at commit
time, it will throw an exception if the order has been changed in the database by a
different user.
The Hibernate version of the business logic would be slightly simpler because
Hibernate objects are automatically detached when the Session is closed. It would
just have to call HibernateTemplate.update() to attach the acknowledged order:
getHibernateTemplate().update(detachedOrder)
In section 13.4 we will look at the details of the AcknowledgeOrderService , which
uses detached objects.
Benefits of using detached objects
Implementing the Optimistic Offline Lock pattern using detached objects has the
following benefits:
 
 
 
 
 
 
Search WWH ::




Custom Search