Grails reverse cascade delete without hasMany or belongsTo
I have two domain-classes.
class UsersAddThese {
String someData
}
and
class TheseAreConstantlyGenerated {
UsersAddThese theProblem
}
Is there some way to delete a UsersAddThese
and automatically delete all the TheseAreConstantlyGenerated
or should I just add in logic to findAllByTheProblem
and then iterate and delete. (I would prefer if 开发者_Python百科it could be done automatically so I can add new Generated classes that refer to UsersAddThese
and not change the delete controller.)
Alternatively, is there some way to just tell GORM, "If something else depends on what I am deleting so as to cause ERROR util.JDBCExceptionReporter - Cannot delete or update a parent row: a foreign key constraint fails
, delete that too - recursively."
Well, it seems like the whole point of referential integrity is to NOT do what you're trying to do in the 2nd part of your question. However, you can add this closure to your UsersAddThese domain class:
def beforeDelete () {
TheseAreConstantlyGenerated.withNewSession {
TheseAreConstantlyGenerated.findAllByTheProblem(this).each {TheseAreConstantlyGenerated thing ->
thing.delete()
}
}
}
That should do the trick.
精彩评论