我正在尝试deep clone
我的域对象。它有大约 10 个一对一的映射,而且还有更多。
我尝试了以下代码段:
def deepClone() {
def copy = this.class.newInstance()
PersistentEntity entity = Holders.grailsApplication.mappingContext.getPersistentEntity(this.class.name)
entity?.persistentProperties?.each { prop ->
if (prop.isAssociation()) {
if (prop.isOneToOne()) {
copy."${prop.name}" = this."${prop.name}"?.deepClone()
} else if (prop.isOneToMany()) {
this."${prop.name}".each {
copy."addTo${StringUtils.capitalize(prop.name)}"(it?.deepClone())
}
}
} else if (prop.name != 'id') {
if (this."${prop.name}" instanceof List) {
this."${prop.name}".each {
copy."addTo${StringUtils.capitalize(prop.name)}"(it)
}
} else {
copy."${prop.name}" = this."${prop.name}"
}
}
}
return copy
}
但是prop.isAssociation
没有找到。有谁知道如何检查关联grails 3.3.11
。这曾经在1.3.7
版本中工作。