Grails - multiple belongsTo of same class with cascading deletion

别说谁变了你拦得住时间么 提交于 2019-12-05 05:22:21

You can add a beforeDelete hook to the Person class and query the other parent. If the other parent does not exist, you can delete the relationship. Note that you are hitting foreign key violations because you probably need to delete both parents, since the relationship has an FK to both of them.

You can also define 2 Relationshipcollections in Person

incomingRelations and outgoingRelations seem usable words to distinguish (if applicable to your domain).

You could define a transient property relationships with a getter only, which returns the union of both relationshipcollections (an immutable to be sure to not modify it / those changes would most likely make no sense)

class Person {
   Relationship incomingRelations
   Relationship outgoingRelations
   static mappedBy = [incomingRelations:'p1', outgoingRelations:'p2']

   static transients = ['relations']

   Set getRelations() {
       //creates a new collection as union of the old ones
       return Collections.unmodifiableSet(this.incomingRelations + this.outgoingRelations)
   }
}
class Relationship {
    static belongsTo = [p1:Person, p2:Person]
}

if doesn't fit i would try the even approach suggested by Miguel-Ping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!