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]工作