Grails

merge() fails on LazyInitializationException - Example attached

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Blocker Blocker
  • Resolution: Fixed
  • Affects Version/s: 1.0.1
  • Fix Version/s: 1.0.2
  • Component/s: None
  • Labels:
    None
  • Environment:
    Grails 1.0.1, Groovy 1.5.4, Java 1.5 update 12
  • Testcase included:
    yes

Description

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.

Activity

Hide
Burt Beckwith added a comment -

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.

Show
Burt Beckwith added a comment - 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.
Hide
Vincent Giguère added a comment -

The bug can also be reproduced when working with entities mapped in GORM.

I have changed the JPA/Hibernate annotated java classes and moved them to grails-app/domain. I experience the same behavior.

Please see BugProof2.zip

Show
Vincent Giguère added a comment - The bug can also be reproduced when working with entities mapped in GORM. I have changed the JPA/Hibernate annotated java classes and moved them to grails-app/domain. I experience the same behavior. Please see BugProof2.zip
Hide
Graeme Rocher added a comment -

The merge() method has been changed to return the new merged instance, so you have to write code like this:

def b = book.merge()

There is an argument that maybe this should be a static method, if there is a massive outcry we can make a static version of the method

Show
Graeme Rocher added a comment - The merge() method has been changed to return the new merged instance, so you have to write code like this: def b = book.merge() There is an argument that maybe this should be a static method, if there is a massive outcry we can make a static version of the method
Hide
Graeme Rocher added a comment -

Bulk closing bunch of resolved issues

Show
Graeme Rocher added a comment - Bulk closing bunch of resolved issues

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: