I'm trying to deep clone
my domain object. It has around 10 one-to-one mapping and further they have more.
I tried the below snippet:
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
}
But prop.isAssociation
is not found. Do anyone know how to check the association in grails 3.3.11
. This used to work in 1.3.7
version.
I resolved this problem with reference from documentation Upgrading from Grails 3.2.x
Here is the new code snippet:
As per the documentation: