Pushing this thread back up...I've taken a look at the idea of manually locking objects and it becomes almost impossible when you combine the idea of cascading in with the locking.
It's my understanding (from reading and a simple test or two) that hibernate.lock(object, LockMode.NONE) doesn't cascade lock all objects into the session. If that's incorrect, please ignore the rest of my post and tell me so.
My troubles come from putting hibernate objects into a session-persisted Struts Form object.
So, my example:
Code:
A a = new A();
B b = hibernate.load(b);
a.setB(b);
dynaForm.set("A", a);
and in the next http request after my session has been closed in the session per request/detached object pattern:
Code:
A a = (A) dynaForm.get("A");
hibernate.saveOrUpdate(a);
This causes a NonUniqueObjectException because of cascades to other properties cause a copy of my "b" object to be loaded several cascade levels deep before the save gets to my "b" property which I loaded in the previous session. ie: it cascades a->c->d->b and that last "b" is the same object (same id) as the "b" loaded in the previous code.
I tried to do both saveOrUpdateCopy and dynamic-insert/dynamic-update=true and several mixtures of the two, but that either didn't work or caused a LazyInitalizationException because "a"'s collections are loaded to check to see if they have changed. As a side note, it seems odd that it would try to load those collections since I've asked it only to generate an update for things that have changed. If they haven't been loaded, they haven't been changed...no?
Looking into Spring, the best case solution seems to be to decouple the form/hibernate objects so we don't run into this problem but it seems like hibernate handles all other edge cases I've run into like this gracefully, so it would be nice if it would do so in this case too. The mantra up to this point has been, just-do-it-with-hibernate-and-let-it-solve-your-problems and now that I've gotten used to it...I want it to solve this problem for me too. :)
Thoughts?
Is anyone else out there doing session persisted struts forms with hibernate objects in them and running into problems like this?
If so, how did you solve the problems?
-mp