Despite the rather religious Open Session in View discusssion, which I realy would like to avoid, I do want to re-open this discussion with one additional variation. In particular, this case involves using Hibernate in a Struts, multi-page, wizard-like scenario.
THE SCENARIO: Sticking with the example used above, the first step would involve loading the base Order object, leaving the (potentially very, very large) items collection to be loaded, if needed, at a later time. The first page is drawn via JSP with some basic fields like description displayed for updating.
In one use case, the user can save or cancel at this point, fulfilling the desire of perhaps only updating the description and by-passing any changes to the items ordered, thereby avoiding a really expensive call to load the items collection. Basic old update() or not here.
However, in use case two, the user would click thru to step two for altering the items, at which point in time the items collection needs to be loaded.
THE ISSUE: In Struts, while using the domain object itself for storing the information, the change in description is done and the detached domain object is "dirty", however, the user still has the option to abort all changes with no updates to the database. Hibernate then, won't let me lock the instance to retrieve the lazy collection. It will let me update() (prematurely by the use case), but at that point, there is no way for the user to abort and revert back to an untouched view.
THE QUESTION: I've tried various lock() LockMode's but get the message that the object has a dirty collection or end up with DB locking issues. So...how do I accomplish this wizardy behavior in Struts/Hibernate without creating a duplicate of the domain object and shuffling data in and out programmatically? I realize there are transactional issues here, but isn't that what the version is for?!? In this case, all versions counters would still be the same by the data, right? So what's the issue?
|