0

我有三个领域模型Type1Type2Details遵循以下关系:

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.detailType2.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 = nulltype1Details*.type2 = type2

4

1 回答 1

0

在尝试了关于如何解决这个问题的所有可能的指令序列之后,我最终得到了这个可行的解决方案。从上面的问题中,我删除了所有Details相关的,Type1因此删除了detail表上与Type1.detail.

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

从上面的代码中,很清楚它是如何完成的。我知道这不是最好的解决方案,但我仍然可以接受更好的解决方案。

于 2012-09-17T11:18:36.530 回答