org.hibernate.HibernateException message re-associated object has dirty collection


Kasmi

I have an application that does the following steps:

  1. Place the object in the session:

    def product = Product.get(1)
    session["product"] = product
    
  2. Execute and Ajax call to update the 1-m relationship, then render a partial template showing the new benefit. These should not be saved because the user may change their mind, so it is called "discarding":

    def product = session["product"]
    
    if ( !product.isAttached() ) {
       product.attach()
    }
    
    product.addToBenefits( new Benefit( title: "xx" ) )
    
    product.discard()
    
    session["product"] = product
    
  3. Attempt to save the object with a save operation.

    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 prevent this from happening so that I can re-attach the object and save it, thus persisting the changes to the product returns collection?

Burt Beckwith

Don't store the object in the session, store the ID, then reload it. You'll pay for it anyway, attachso you won't save any money and cause this problem, as well as wasting server memory, which will affect scalability.

Related


org.hibernate.HibernateException: Two open sessions

Lucie Leigh Allen I am new to hibernate technology and I am getting the following exception: org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions I get this when I try to add a new row in the database. My setup/cod

org.hibernate.HibernateException: Missing table: all

Dennis When I run the project under help, I get Gradlean exception: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class com.somepackage.Application: Invocation of init method failed; ne

org.hibernate.HibernateException: Two open sessions

Lucie Leigh Allen I am new to hibernate technology and I am getting the following exception: org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions I get this when I try to add a new row in the database. My setup/cod

org.hibernate.HibernateException: Missing table: all

Dennis When I run the project under help, I get Gradlean exception: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class com.somepackage.Application: Invocation of init method failed; ne