如何在 Grails 中复制域对象?

Posted

技术标签:

【中文标题】如何在 Grails 中复制域对象?【英文标题】:How can I duplicate a domain object in Grails? 【发布时间】:2013-07-10 23:44:02 【问题描述】:

我想复制一个域对象。完成此任务的最简单方法是什么?

我意识到我可以创建一条新记录,然后遍历每个字段,逐字段复制数据 - 但我认为必须有更简单的方法来做到这一点......

在 Rails 中有一种简单的方法可以做到这一点:

#rails < 3.1
new_record = old_record.clone

#rails >= 3.1
new_record = old_record.dup

在 Grails 中是否有任何等价物?

【问题讨论】:

【参考方案1】:

我已经改编了一段代码,可以对领域类进行深度克隆。我一直在我的系统中使用它并且效果很好(在大多数情况下)。下面的代码是在http://grails.1312388.n4.nabble.com/Fwd-How-to-copy-properties-of-a-domain-class-td3436759.html中找到的改编代码

在我的应用程序中,用户可以选择另存为某些类型的对象,我使用 deepClone 来执行此操作。

您可以指定“不可克隆”属性。为此,您需要指定一个静态映射(在您的类中),其中包含您不想克隆的属性,例如:

static notCloneable = ['quoteFlows','services']
static hasMany = [quotePacks: QuotePack, services: Service, clients: Client, quoteFlows: QuoteFlow]


static Object deepClone(domainInstanceToClone) 

    //TODO: PRECISA ENTENDER ISSO! MB-249 no youtrack
    //Algumas classes chegam aqui com nome da classe + _$$_javassist_XX
    if (domainInstanceToClone.getClass().name.contains("_javassist"))
        return null

    //Our target instance for the instance we want to clone
    // recursion
    def newDomainInstance = domainInstanceToClone.getClass().newInstance()

    //Returns a DefaultGrailsDomainClass (as interface GrailsDomainClass) for inspecting properties
    GrailsClass domainClass = domainInstanceToClone.domainClass.grailsApplication.getDomainClass(newDomainInstance.getClass().name)

    def notCloneable = domainClass.getPropertyValue("notCloneable")

    for(DefaultGrailsDomainClassProperty prop in domainClass?.getPersistentProperties()) 
        if (notCloneable && prop.name in notCloneable)
            continue

        if (prop.association) 

            if (prop.owningSide) 
                //we have to deep clone owned associations
                if (prop.oneToOne) 
                    def newAssociationInstance = deepClone(domainInstanceToClone?."$prop.name")
                    newDomainInstance."$prop.name" = newAssociationInstance
                 else 

                    domainInstanceToClone."$prop.name".each  associationInstance ->
                        def newAssociationInstance = deepClone(associationInstance)

                        if (newAssociationInstance)
                            newDomainInstance."addTo$prop.name.capitalize()"(newAssociationInstance)
                    
                
             else 

                if (!prop.bidirectional) 

                    //If the association isn't owned or the owner, then we can just do a  shallow copy of the reference.
                    newDomainInstance."$prop.name" = domainInstanceToClone."$prop.name"
                
                // @@JR
                // Yes bidirectional and not owning. E.g. clone Report, belongsTo Organisation which hasMany
                // manyToOne. Just add to the owning objects collection.
                else 
                    //println "$prop.owningSide - $prop.name - $prop.oneToMany"
                    //return
                    if (prop.manyToOne) 

                        newDomainInstance."$prop.name" = domainInstanceToClone."$prop.name"
                        def owningInstance = domainInstanceToClone."$prop.name"
                        // Need to find the collection.
                        String otherSide = prop.otherSide.name.capitalize()
                        //println otherSide
                        //owningInstance."addTo$otherSide"(newDomainInstance)
                    
                    else if (prop.manyToMany) 
                        //newDomainInstance."$prop.name" = [] as Set

                        domainInstanceToClone."$prop.name".each 

                            //newDomainInstance."$prop.name".add(it)
                        
                    

                    else if (prop.oneToMany) 
                        domainInstanceToClone."$prop.name".each  associationInstance ->
                            def newAssociationInstance = deepClone(associationInstance)
                            newDomainInstance."addTo$prop.name.capitalize()"(newAssociationInstance)
                        
                    
                
            
         else 
            //If the property isn't an association then simply copy the value
            newDomainInstance."$prop.name" = domainInstanceToClone."$prop.name"

            if (prop.name == "dateCreated" || prop.name == "lastUpdated") 
                newDomainInstance."$prop.name" = null
            
        
    

    return newDomainInstance

【讨论】:

在返回对象之前,我试图保存它,但它显示为 null...如何保存这个新对象 您好@roanjain,我将查看此代码以检查它是否已更新。但我可以说我成功地使用它克隆了几个类。【参考方案2】:

没有。已请求http://jira.grails.org/browse/GRAILS-3532。有人在该问题上添加了一些代码,但可能对您有所帮助。

【讨论】:

以上是关于如何在 Grails 中复制域对象?的主要内容,如果未能解决你的问题,请参考以下文章

Grails:如何在多对多映射中查询对象?

如何使用 Grails 4 JSON 视图呈现域对象的地图

Grails 无法保存对象,会话中已存在? [复制]

Grails:如何从控制器逻辑返回错误消息(即不是源自域属性验证)?

如何在 grails 域子类中添加对继承属性的约束

获取行时的 grails 验证