如何覆盖Grails GORM中关系的级联删除? [英] How do I override the cascade delete for a relation in Grails GORM?

查看:279
本文介绍了如何覆盖Grails GORM中关系的级联删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Grails的GORM部分遇到一些问题。在数据库中,我有两个表模板报表 >。在GORM级别,我有两个Domain类 Template Report ;

  class Template {

static hasMany = [reports:Report]

...
}

  
static belongsTo = [template:Template]

...
}

默认行为似乎是在删除 Template 时删除将被级联,所有报告 s也将被删除。
在数据库级别上,我尝试使报告中的 template_id -table成为 ON DELETE SET NULL 外键,但没有奏效。

是否有一些方法可以覆盖级联删除?

解决 code> static mapping = {
报告级联:'none'
}

为了能够毫无问题的删除 Template ,除了 Report 类也是必须的: / p>

  static constraints = {
template(nullable:true)
}


I'm having some problems with the GORM part of Grails. I am using Grails 1.3.4, together with H2.

In the database I have two tables template and report. On the GORM-level I have the two Domain classes Template and Report;

class Template {

static hasMany = [reports: Report]

...
}

and

class Report {

static belongsTo = [template: Template]

...
}

Default behaviour seems to be that when a Template is deleted, the deletion will be cascaded so that all Reports that it has will be deleted as well. On the database level I tried to make the template_id-column in the report-table be a ON DELETE SET NULL foreign key , but that didn't work.

Is there some way to override the cascade delete?

解决方案

The following should be added in the Template class:

static mapping = {
  reports cascade: 'none'
}

to be able to delete Templates without problems, this addition to the Report class is also necessary:

static constraints = {
  template(nullable: true)
}

这篇关于如何覆盖Grails GORM中关系的级联删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆