【问题标题】:How do I override the cascade delete for a relation in Grails GORM?如何覆盖 Grails GORM 中关系的级联删除?
【发布时间】:2010-11-09 06:54:34
【问题描述】:

我在使用 Grails 的 GORM 部分时遇到了一些问题。我正在使用 Grails 1.3.4 和 H2。

在数据库中,我有两个表 templatereport。在 GORM 级别上,我有两个域类 TemplateReport

class Template {

static hasMany = [reports: Report]

...
}

class Report {

static belongsTo = [template: Template]

...
}

默认行为似乎是当 Template 被删除时,删除将被级联,以便它拥有的所有 Reports 也将被删除。 在数据库级别上,我尝试使 report-表中的 template_id-列成为 ON DELETE SET NULL 外键,但这并没有不行。

有没有办法覆盖级联删除?

【问题讨论】:

    标签: grails relational-database grails-orm foreign-key-relationship h2


    【解决方案1】:

    应在Template 类中添加以下内容:

    static mapping = {
      reports cascade: 'none'
    }
    

    为了能够毫无问题地删除Templates,Report 类的这个添加也是必要的:

    static constraints = {
      template(nullable: true)
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 2012-09-07
      相关资源
      最近更新 更多