tags:

views:

171

answers:

1

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 individually, but how do I set belongsTo on each Location object as I go?

So to recap:

  1. How do I clear a list at the many end of a one-to-many collection?
  2. How do I associate independent objects with a parent object and persist them individually?

Thanks

+1  A: 

For #1 you can clear the collection (it's a Set by default):

activePerson.locations.clear()

For #2 use addToLocations:

activePerson.addToLocations(location)

and when you save the person the location<->person relationship will be updated.

Burt Beckwith
@Burt, thanks (again) for your answer, I owe you a beer. This does however imply I have to persist the Person object, which I was trying to avoid. Is there a sensible alternative or should I just live with updating a record which hasn't really changed?
Simon