开发者

Grails reverse cascade delete without hasMany or belongsTo

开发者 https://www.devze.com 2023-03-04 18:49 出处:网络
I have two domain-classes. class UsersAddThese { String someData } and class TheseAreConstantlyGenerated {

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消