Search code examples
grailsgrails-ormcascadecascading-deletes

Grails belongsTo cascade on delete when belongsTo specifies multiple classes?


class Owner {
    static hasMany = Dog
}
class Sitter {
    static hasMany = Dog
}
class Dog {
    static belongsTo = [Owner, Sitter]
}

My question is: If I create a Dog instance D, a Owner instance O, a Sitter instance S and associate D with both O and S, what happens to O when S gets deleted? Would O still have D? Since it's a cascade-delete, both S and D would get deleted, right? When what happens to O? Would it still have D?


Solution

  • I have tested it, it follows the cascade rule: if you delete Owner, Dog will be deleted by cascade, but Sitter will remain.

    And it's reasonable: Sitter is independent with Owner. It's illogical that Sitter should be deleted along with Owner, just because he has some common properties with Owner.