I have an application which performs the following steps:
Places object in session:
def product = Product.get(1)
session["product"] = product
Performs and Ajax call to update a 1-m relationship, and then renders a partial template which displays the new benefits. These should not yet be saved as the user may change their mind, so discard is called:
def product = session["product"]
if ( !product.isAttached() ) {
product.attach()
}
product.addToBenefits( new Benefit( title: "xx" ) )
product.discard()
session["product"] = product
Attempts to save the object in a save action.
def product = session["product"]
if ( !product.isAttached() ) {
product.attach()
}
product.save()
At this point we get the following exception:
org.springframework.orm.hibernate3.HibernateSystemException: reassociated object has dirty collection; nested exception is org.hibernate.HibernateException: reassociated object has dirty collection
Is there anyway to stop this happening, so that I can re-Attach the object, and save it, thus persisting the changes to the products benefits collection?
Don't store the object in the session, store the id, and reload it instead. You're incurring that cost anyway with attach
, so you're not saving anything, and causing this problem, plus wasting server memory, which will affect scalability.