3
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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Paul
  • 2,115
  • 3
  • 17
  • 14
  • I think it deletes the instance. However you could actually test it quite easily in a separate project. I'd be curious to know the definite answer myself. – julx Apr 03 '11 at 23:48

1 Answers1

3

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.

Hoàng Long
  • 10,746
  • 20
  • 75
  • 124
  • 1
    I meant, if Sitter gets deleted, would Owner still have a Dog? Or in your example, if Owner gets deleted, would Sitter still have a Dog? – Paul Apr 04 '11 at 23:02
  • 1
    @Pau: if Sitter gets deleted, the dog also get deleted by cascade. So the Owner will not have the Dog. The same for the other case. – Hoàng Long Apr 05 '11 at 01:07
  • Is there a way to prevent Dog getting deleted if Owner get deleted? since Sitter still has the Dog. – RRK May 02 '13 at 22:15
  • @RRK: you can customize hibernate with xml config, or use beforeDelete trigger to remove the Dog from Owner list before delete owner – Hoàng Long May 03 '13 at 09:19
  • there maybe some other Grails-specific ways that I haven't thought about – Hoàng Long May 03 '13 at 09:19