Grails 反向级联删除,不带 hasMany 或 BelongsTo

发布于 2024-11-06 03:15:48 字数 528 浏览 10 评论 0原文

我有两个域类。

class UsersAddThese {
  String someData
}

是否

class TheseAreConstantlyGenerated {
  UsersAddThese theProblem
}

有某种方法可以删除 UsersAddThese 并自动删除所有 TheseAreConstantlyGenerate 或者我应该将逻辑添加到 findAllByTheProblem 然后迭代并删除。 (我希望它可以自动完成,这样我就可以添加引用 UsersAddThese 的新生成类,而不是更改删除控制器。)

或者,是否有某种方法可以告诉 GORM,“如果有什么事情否则取决于我要删除的内容,从而导致错误 util.JDBCExceptionReporter - 无法删除或更新父行:外键约束失败,也将其删除 - 递归。”

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 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."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2024-11-13 03:15:48

嗯,看起来引用完整性的全部要点是不要做你在问题的第二部分中试图做的事情。但是,您可以将此闭包添加到您的 UsersAddThese 域类中:

def beforeDelete () {
    TheseAreConstantlyGenerated.withNewSession {
      TheseAreConstantlyGenerated.findAllByTheProblem(this).each {TheseAreConstantlyGenerated thing ->
        thing.delete()
      }
    }
}

这应该可以解决问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文