How do I override the cascade delete for a relation in Grails GORM?

我怕爱的太早我们不能终老 提交于 2020-01-14 19:38:27

问题


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?


回答1:


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)
}


来源:https://stackoverflow.com/questions/4131174/how-do-i-override-the-cascade-delete-for-a-relation-in-grails-gorm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!