Grails GORM级联删除关闭
我有三个域模型Grails GORM级联删除关闭,grails,gorm,cascade,Grails,Gorm,Cascade,我有三个域模型Type1、Type2和Details,它们之间的关系如下: class Type1 { static hasMany = [detail: Detail] } class Type2 { static hasMany = [detail: Detail] } class Detail { Type1 type1 Type2 type2 static belongsTo = [Type1, Type2] static constraints = {
Type1
、Type2
和Details
,它们之间的关系如下:
class Type1 {
static hasMany = [detail: Detail]
}
class Type2 {
static hasMany = [detail: Detail]
}
class Detail {
Type1 type1
Type2 type2
static belongsTo = [Type1, Type2]
static constraints = {
type1(nullable:true)
type2(nullable:true)
}
}
问题是,每当Type1
转换为Type2
时,我无法将Type1.detail
转换为Type2
(注意:Type1和Type2只是java.lang.Object
的子对象)。换句话说(在控制器中):
问题是,只有更新
type1Details
,我们如何才能设置type1Details*.type1=null
和type1Details*.type2=type2
?在尝试了关于如何解决此问题的所有可能的指令序列之后,我最终得到了这个有效的解决方案。从上述问题中,我删除了与Type1
相关的所有Details
,从而删除了detail
表中与Type1.Details
相关的记录
Type1 type1 = Type1.get(params.id)
Type2 type2 = new Type2()
// transfer other properties of type1 to type2
type1.detail.each {
Detail element = (Detail) it
element.type1 = null
element.type2 = type2
}
type1.detail.clear()
if(type2.save(flush:true)) {
type1.delete(flush:true)
}
从上面的代码可以清楚地看出它是如何完成的。我知道这不是最好的解决方案,但我仍然可以接受更好的解决方案
Type1 type1 = Type1.get(params.id)
Type2 type2 = new Type2()
// transfer other properties of type1 to type2
type1.detail.each {
Detail element = (Detail) it
element.type1 = null
element.type2 = type2
}
type1.detail.clear()
if(type2.save(flush:true)) {
type1.delete(flush:true)
}