can GORM duplicate whole object?

我的梦境 提交于 2019-12-11 08:36:16

问题


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:

  1. Null the id.
  2. Update fields that should differ in the copy.
  3. Detach the object in question.
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!