我在使用 Grails 的 GORM 部分时遇到一些问题。我正在使用 Grails 1.3.4 和 H2。
在数据库中,我有两个表模板和报告。在 GORM 级别,我有两个域类 Template
和 Report
;
class Template {
static hasMany = [reports: Report]
...
}
和
class Report {
static belongsTo = [template: Template]
...
}
默认行为似乎是,当删除Template
时,删除操作将被级联,以便它拥有的所有Report
也将被删除。
在数据库级别,我尝试将 report 表中的 template_id 列设置为 ON DELETE SET NULL 外键,但这并没有不起作用。
有什么方法可以覆盖级联删除吗?
最佳答案
应在 Template
类中添加以下内容:
static mapping = {
reports cascade: 'none'
}
为了能够毫无问题地删除 Template
,向 Report
类添加此内容也是必要的:
static constraints = {
template(nullable: true)
}
关于grails - 如何覆盖 Grails GORM 中关系的级联删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4131174/