我有以下3个班级
class User {
static hasMany = [coupons: Coupon]
}
class Coupon {
static belongsTo = [affiliate: Affiliate]
}
class Affiliate {
static hasMany = [coupons: Coupon]
}
如何设置级联以便我可以删除特定的优惠券,它将从联属会员和用户的列表中删除。我一直收到“无法删除或更新父行”
我错过了什么吗?
答案 0 :(得分:4)
您需要从父集合中删除优惠券。您可以在删除优惠券的服务中手动执行此操作,也可以使用休眠事件来执行此操作。我不认为你可以有一个级联这样做。你可以得到的最接近的是'delete-orphan',但是你仍然需要打破这种关联才能发挥作用。
参见手册第5.5.1节。
基本上,您可以在Coupon类中添加'beforeDelete'方法,从其父项中删除其引用。
在删除优惠券之前,你已经猜到了grails足够聪明,可以启动beforeDelete方法。这是打破协会的好地方。无论是在服务或域类中“手动”执行,还是使用hibernate事件,代码都将基本相同。确保为它编写集成测试...
答案 1 :(得分:2)
尝试在用户
中添加此内容static mapping = {
cupons cascade: "all-delete-orphan"
}