Search code examples
grailsgrails-ormgrails-domain-classall-delete-orphan

How can a Domain Class cascade 'all-delete-orphan' to a child it has no reference to?


In Grails you can have a child class:

class Child {
    Father father
    static belongsTo = [Father, Mother]
}

With two parent classes

class Mother{
}

class Father { 
}

It appears that if I father.delete(), then Grails throws a database error saying that the Father can't be deleted because the child is still around.

How do I cascade all-delete-orphan the Child if the Father class doesn't have a direct reference to the Child class?


Solution

  • Make it bi-directional using hasMany.

    class Mother{
      static hasMany = Child
    }
    class Father{
      static hasMany = Child
    }
    

    Doing this should make the cascading work such that when you delete one of the parents the child will also be deleted.