【发布时间】:2020-04-21 06:57:25
【问题描述】:
我正在尝试deep clone 我的域对象。它有大约 10 个一对一映射,而且还有更多。
我试过下面的sn-p:
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 版本中工作。
【问题讨论】:
标签: grails clone grails-domain-class grails-3.3.x