Reputation: 3007
I have the following 3 classes
class User {
static hasMany = [coupons: Coupon]
}
class Coupon {
static belongsTo = [affiliate: Affiliate]
}
class Affiliate {
static hasMany = [coupons: Coupon]
}
How do I setup cascading so that I can delete a specific Coupon and it will be removed from the lists in Affiliate and User. I keep getting "Cannot delete or update a parent row"
Am I missing something?
Upvotes: 1
Views: 4314
Reputation: 9736
try adding this in User
static mapping = {
cupons cascade: "all-delete-orphan"
}
Upvotes: 2
Reputation: 120178
You need to remove the Coupon from the parent collections. You can either do that manually in the service where you delete the Coupon, or you can user hibernate events to do so. I dont think you can have a cascade do this. The closest you can get is 'delete-orphan', but you still need to break the association for that to work.
See section 5.5.1 of the manual.
Basically, you can add a 'beforeDelete' method to your Coupon class that removes its reference from its parents.
grails is smart enough to fire the beforeDelete method, you guessed it, before the coupon is deleted. Its a good place to break the associations.
Whether you do it 'manually' in a service or domain class, or you use hibernate events, the code will be almost the same. Make sure you write an integration test for it...
Upvotes: 4