Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails GORM在删除时将多对一关系中的外键设置为null_Grails_Constraints_Gorm - Fatal编程技术网

Grails GORM在删除时将多对一关系中的外键设置为null

Grails GORM在删除时将多对一关系中的外键设置为null,grails,constraints,gorm,Grails,Constraints,Gorm,我的域名: Company { String name } Contact { String name Company compa static constraints = { compa (nullable: true) } } 如果某公司具有联系人的外键约束,则无法删除该公司。在删除公司时,我希望删除可以工作,并且compa属性设置为null 是否存在这样做的约束?有没有比我正在尝试的更好的方法呢?试试看,也许还有其他选择。我没有测试这段代码,

我的域名:

Company {
   String name
}

Contact {
   String name
   Company compa

   static constraints = {
      compa (nullable: true)
   }
}
如果某公司具有联系人的外键约束,则无法删除该公司。在删除公司时,我希望删除可以工作,并且compa属性设置为null


是否存在这样做的约束?有没有比我正在尝试的更好的方法呢?

试试看,也许还有其他选择。我没有测试这段代码,只是想给你一个想法

在Company.groovy中:

 def beforeDelete() {
      Contact.withNewSession {
          Contact.findAllByCompany(this).each {
            it.company = null
            it.save()
          }
      }
 }

试试看,也许还有其他选择。我没有测试这段代码,只是想给你一个想法

在Company.groovy中:

 def beforeDelete() {
      Contact.withNewSession {
          Contact.findAllByCompany(this).each {
            it.company = null
            it.save()
          }
      }
 }
谢谢!:)它在FindallByCompany上使用[lock:true]有效谢谢!:)它在findAllByCompany上使用[lock:true]工作