2013-07-12 56 views
7

我想製作一個域對象的副本。什麼是最簡單的方法來完成這個?如何在Grails中複製域對象?

我知道我可以創造一個新的記錄,然後遍歷每個複製數據場逐場域 - 但我想一定有這樣做的更簡單的方法...

在Rails有一個簡單的方法來做到這一點:

#rails < 3.1 
new_record = old_record.clone 

#rails >= 3.1 
new_record = old_record.dup 

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

回答

7

我已經修改了一段代碼,可以對域類進行深層克隆。我一直在使用我的系統,它工作得很好(在大多數情況下)。下面的代碼是在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 
} 
+0

回來時,我試圖挽救它的對象之前,但。 ..how可以保存這個新對象 – roanjain

+0

你好@roanjain,我會檢查這個代碼來檢查它是否被更新。 但我可以說我成功地使用它來克隆幾個類。 – cantoni