This question is in two parts, first part is about clearing a list and second part is about assigning an owner to an object.

I have a one-to-many relationship between two domain objects in my model in Grails. The relationship looks like this...

class Person {

    static hasMany = [authorities: Role, locations: Location]
}

class Location {

    static belongsTo = Person

}

In my app the locations list on Person gets completely refreshed and replaced with a new list on a user action. What's more I get the list of Location objects independent of the associated Person. I resolve which person to apply them to by retrieving the currently logged in user, which I call activePerson and is fine for my purposes.

What I want to do is delete all the existing locations on activePerson and insert all the new ones, associating them with the activePerson as I go. I have a bunch of properties on Person which I don't want to persist at this point, so I want to avoid saving the whole parent object just because locations children have changed.

I thought of iterating throught the activePerson.locations list and deleting them one by one and relying on GORM/Hibernate to batch the queries together and flush at the end. This seems like brute force, although it might work. I was expecting there to be a clearLocations method or something similar, but I can't find one.

If I do just replace the activePerson.locations with a new list and call activePerson.save(flush:true), will GORM handle the delete of the existing rows?

Secondly I want to put the new Location objects onto activePerson. Again I could populate the activePerson.locations and save the whole thing, but I would like to avoid that if I can. I can save them i

Edit
Report