我有三个领域模型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.detail
在Type2.detail
任何时候Type1
被转换为Type2
(注意:Type1 和 Type2 只是一个孩子java.lang.Object
)。换句话说(在控制器中):
Type1 type1 = Type1.get(params.id)
List type1Details = Detail.findAllByType1(type1)
type1.detail.clear()
Type2 type2 = new Type2()
// transfer other properties of type1 to type2
type1Details.each { type2.addToDetail(it) }
if(type2.save(flush:true) {
type1.save(flush:true)
type1.delete(flush:true)
}
问题是,只有更新type1Details
,我们如何设置type1Details*.type1 = null
和type1Details*.type2 = type2
?