0
votes

I have the following grails domain classes:

A {
 belongsTo = [b: B]
}

B {
 hasMany = [aClasses: A]
}

In a GSP view I have a form where the user can select many A instances to connect them with a B instance. When the user submits the form the Controller which handles the form receives something like this:

params: [aClasses: [123,124]]

The user may have removed previously added aClasses from B so I tried to clear the aClasses of B first and then perform addTo but I get a java.util.ConcurrentModificationException.

Here is what I do in my Controller Action:

def update() {
B b = B.get(params.id)
b.properties = params
b.aClasses.clear()

def newAs = params.aClassIds ? [] + params.aClasses : []
newAs.each {
 A a = A.get(it)
 b.addToAClasses(a)
}
b.save(flush: true)

}

Is there a way to make this work?

1

1 Answers

0
votes

Is this running in a service or controller? I've done something similar before but the logic was in a service and it worked. This kind of database access should run in a service because they are transactional.

The example that worked is :

    if(!clone.hasErrors())
    {
        def projectTasks = Task.withCriteria {
            project {
              eq('id', project_id.toInteger())
            }
          }
        projectTasks.each{ task -> 
            clone.addToTasks(task)
        }
    }

    clone.save(flush : true)