正确实现clone()对于域类来复制Grails域实例

时间:2013-11-26 15:05:35

标签: grails clone gorm

我有几个用户界面包含的域类 一个“重复”命令。作为执行这些命令的一部分, 我在相应的域类中实现了clone()个方法。

我一直在努力纠正不正当实施的坏习惯 clone()(一般情况下)基于使用“new”而不是"super.clone()," 所以,一旦我想到为我的Grails域做同样的事情 类,我想知道如何使用super.clone()来获取克隆 与GORM / Hibernate持久性交互。特别是,我是 想知道处理隐式“id”属性的正确方法。如果 我只是super.clone(),什么都不做,后来尝试save() 克隆实例,它会正常工作(创建一个新的持久性 进入?)或会出现某种错误或无声失败的结果?

复制Grails域的正确或首选方法是什么 实例

2 个答案:

答案 0 :(得分:15)

将以下方法添加到所有域实现它的接口GormInstanceApi的metaClass。  :

def cloneForDomains={def cloned=delegate.class.newInstance();
                 cloned.properties=delegate.properties;
                return cloned;}

然后:

org.grails.datastore.gorm.GormInstanceApi.clone=cloneForDomains ;

恭喜!现在,您可以使用clone方法,例如savedelete .....

使用案例

Person p=Person.get(1);
Person cloned=p.clone(); 
 cloned.id=null; 
cloned.save();

更新:您也可以循环所有域类:

grailsApplication.getDomainClasses().each{cls->
             cls.metaClass.clone=cloneForDomains
         }
深色克隆的

更新:

  grailsApplication.getDomainClasses().each{cls->
                 cls.metaClass.clone={
                                  return deepClone(delegate);   
                            }
   }

已知deepClone是一种方法如下:

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
     }

答案 1 :(得分:10)

我对这种方法有更多好运:

YourDomainClass clonedObject = new YourDomainClass(objectToClone.properties)
相关问题