Following up on a mail to user@grails.codehaus.com entitled merge() fails on LazyInitializationException
I am unable to merge() a persistent object stored in the session. When doing so, it fails on a LazyInitializationException.
The merge() is done inside a transactional service method, but the same behavior occurs when merging from the controller.
I have attached a simple projet that duplicates the problem.
- Click on the QuestionController link
- Select a question from the drop down
- Click "Next"
- Click "showTheBug" . You will have a stack trace.
I'm not sure how much this affects this particular problem, but I'm getting a NonUniqueObjectException "a different object with the same identifier value was already associated with the session" when it gets to the session.lock() call in MergePersistentMethod. merge() doesn't update the entity parameter, it returns the updated instance, so
session.merge(target);
session.lock(target, LockMode.NONE);
...
return target;
should be
Object merged = session.merge(target);
session.lock(merged, LockMode.NONE);
...
return merged;
but this wouldn't work for Grails since it's an instance method.