2

I'm using the Grails 3 grails.rest.Resource annotation in the following way:

@Resource(uri='/parent')
class Parent {
    static hasMany = [children: Child]
}

class Child {
    String property1
    String property2

    static belongsTo = [parent: Parent]
}

Now, let's say, I have a Parent object with 2 children and I issue a GET request for that Parent with something like GET localhost:8080/parent/1. The JSON response will look something like (assuming I'm not using the default JSON renderer, but that may not actually be relevant):

{
     "id": "1",
     "children": [
         {
             "id": "1",
             "property1": "prop1",
             "property2": "prop2"
         },
         {
             "id": "2",
             "property1": "prop1",
             "property2": "prop2"
         }
     ]
}

I want to be able to issue a PUT request with a JSON payload that looks something like...

{
    "id": "1",
    "children": []
}

... and have all of the children of the Parent be deleted.

Is this possible without implementing a custom rest controller? The children in this case are dependent objects and ideally this would be a single update as opposed to issuing separate DELETE requests on a rest controller for Child objects, and it would also be nice to not have to implement custom code to do the child delete =D.

Mike
  • 33
  • 3
  • The use case I mentioned seems to work if I get rid of the `static belongsTo = [parent: Parent]` on the Child domain class. I think this was a misunderstanding of how the relationship is managed when using `belongsTo`. – Mike Jun 28 '17 at 23:58
  • I have the same issue, but when I get rid of 'belongsTo', Hibernate validator wants a linking table which doesn't exist. I also need the child to have a reference to its parent. Any idea how to fix this? – RMorrisey Aug 08 '17 at 15:36

0 Answers0