问题
we are using grails to develop some web application For Domain class that have child class , I'm wondering if we can duplicate whole object including all child object that belong to Parent ?
Thank you
回答1:
As given in the comments, you can extend gorm with a clone
method.
However, a very simple solution if you don't want to mess with the gorm api is to detach the existing object and just "resave" it. Note that this won't perform a deepClone
.
Steps:
- Null the id.
- Update fields that should differ in the copy.
- Detach the object in question.
- Save it.
Code example, assuming a domain class Region
which has a unique name
property that needs to change before saving:
def copyRegion(Region region, String newName) {
region.id = null
region.name = newName
region.discard()
if (region.save()) {
// handle success
} else {
// handle error
}
}
See also this question about disconnecting an object.
来源:https://stackoverflow.com/questions/26031825/can-gorm-duplicate-whole-object