How do I prevent child properties from being updated?

349 Views Asked by At

When I use RestfulController in Grails to save data for an object, how can I prevent the client from applying changes to a related child object?

Given the following domain classes:

class Language {
    String name
}

class TranslationText {
    Language language
    String text
}

And the following POST data for a TranslationText:

{
    "language": { "id": 1, "name": "InvalidName" },
    "text": "Some Text"
}

Here, I want to reference an existing Language resource (with ID=1), but I don't want the name to be altered by the client.

How can I save this resource with the text and language (based on ID), but discard the invalid language name property?

I want to modify RestfulController in the most minimal way possible, preserving default behavior as much as I can.

1

There are 1 best solutions below

0
bassmartin On

I think you need to configure the 'cascade' mapping property. This will tell GORM to evict the linked instance, so it won't be in the Hibernate session, changed to a new name and flushed to the DB :

class TranslationText {
    Language language
    String text

    static mapping = {
        language cascade: 'evict'
    }
}

ref : http://docs.grails.org/3.1.x/ref/Database%20Mapping/cascade.html