Search code examples
grailsone-to-manycascadeall-delete-orphan

Grails data binding one-to-many relationship


I am trying to take advantage of Grails' ability to handle data binding for a one-to-many association. I am able to successfully assigning to the relationship but removing single elements or all of them is not working.

Here is an example of what my model looks like:

class Location {
    Float latitude
    Float longitude
}

class Route {
    Location start
    Location stop

    static belongsTo: [courier: Courier]
}

class Courier {
    String name
    static hasMany = [pickups: Location]
}

class ScheduledCourier extends Courier {
    static hasMany = [routes: Route]

    static mapping = {
        routes(cascade: 'all-delete-orphan')
    }
}

When creating a new ScheduledCourier object via a website, I can pass a list of Routes to automatically bind with markup like this:

<input type="hidden" name="routes[0].latitude" value="5" />
<input type="hidden" name="routes[0].longitude" value="5" />
<input type="hidden" name="routes[1].latitude" value="10" />
<input type="hidden" name="routes[1].longitude" value="10" />

This works for me just fine in my controller:

class CourierController {
    // Very simplistic save
    def saveScheduled = {
        def courier = new ScheduledCourier(params)
        courier.save()
    }

    // Very simplistic update
    def update = {
        def courier = Courier.get(params.id)
        courier.properties = params
        courier.save()
    }
}

If I use the following markup instead, I can step through the debugger and see that the routes property is now [] and the object saves fine but the records are not removed from the database.

<input type="hidden" name="routes" value="" />

In addition, if I sent markup like this:

<input type="hidden" name="routes[0].latitude" value="5" />
<input type="hidden" name="routes[0].longitude" value="5" />

courier.routes will not be updated to only contain the 1 object.

Has anyone seen this behavior?

This is Grails 1.3.7...at least for now.

Wrote an integration test that reproduces this behavior:

public void testCourierSave() {
    def l1 = new Location(latitude: 5, longitude: 5).save(flush: true)
    def l2 = new Location(latitude: 10, longitude: 10).save(flush: true)

    def params = ["name": "Courier", "pickups[0].id": l1.id, "pickups[1].id": l2.id,
        "routes[0].start.id": l1.id, "routes[0].stop.id": l2.id,
        "routes[1].start.id": l2.id, "routes[1].stop.id": l1.id]

    def c1 = new ScheduledCourier(params).save(flush: true)

    assertEquals(2, c1.routes.size())

    params = [routes: ""]
    c1.properties = params
    c1.save(flush: true)
    c1.refresh()    // Since the Routes aren't deleted, this reloads them

    assertEquals(0, c1.routes.size())    // Fails

    assertEquals([], Route.findAllByCourier(c1))    // Fails if previous assert is removed
}

Solution

  • I wonder if the following is happening:

    When passing the params [routes:""] the framework is ignoring it as it's just an empty string.

    Similarly <input type="hidden" name="routes[0].latitude" value="5" /> probably just updates the zeroth route entry in the collection, the others aren't deleted because all you've told it is that the latitude value of the zeroth route should be 5, not that this is should now be the only route in the collection.

    To get the effect you want, you'll need to add a routes.clear() before binding the parameters.

    To control when the state of the model is persisted to the database you can use Spring transactionality which is available in Grails. This would allow you to revert to the original state of the object if subsequent processing failed. eg:

    Courier.withTransaction {status ->
    
      // Load the courier
    
      //clear the existing routes
    
      // bind the new properties
    
      // perform extra processing
    
      //if the object is invalid, roll back the changes
      status.setRollbackOnly()
    
    }