Hello I have a question about beforeInsert/beforeUpdate in Grails. I can't get a simple parent/child use case to work. The child updates the parent grandchildren counter. See the model below:
class Parent{
int grandchildren
def beforeUpdate(){
}
}
class Child{
Parent parent
int childCount
def beforeInsert(){
// 1: Works, beforeUpdate is called in Parent
parent.grandchildren+=childCount
parent.save()
}
def beforeUpdate(){
// 2: Does not work, grandchildren not updated and beforeUpdate not called in Parent
parent.grandchildren+=childCount
parent.save()
}
}
2) This is documented by the grails documentation. BeforeUpdate is called while the session is flushing thus adding a update while updates are being handled leads to unexpected behaviour. The documentation says to use withNewSession to workarround this, but I can't figure out to do this without having the parent instance registered in two different sessions.
My question is how to implement this. I don't want to use HQL to update the parent entity, because I need beforeUpdate to be called.