If I have a List in a Grails domain class, is there a way to override the addX() and removeX() accessors to it?
In the following example, I’d expect MyObject.addThing(String) to be called twice. In fact, the output is:
Adding thing: thing 2
class MyObject {
static hasMany = [things: String]
List things = []
void addThing(String newThing) {
println "Adding thing: ${newThing}"
things << newThing
}
}
class BootStrap {
def init = { servletContext ->
MyObject o = new MyObject().save()
o.things << 'thing 1'
o.addThing('thing 2')
}
def destroy = {
}
}
Moving my comment to an answer:
Try using the built-in addToThings as documented here:
http://www.grails.org/doc/latest/ref/Domain%20Classes/addTo.html