我想制作一个域对象的副本。什么是最简单的方法来完成这个?如何在Grails中复制域对象?
我知道我可以创造一个新的记录,然后遍历每个复制数据场逐场域 - 但我想一定有这样做的更简单的方法...
在Rails有一个简单的方法来做到这一点:
#rails < 3.1
new_record = old_record.clone
#rails >= 3.1
new_record = old_record.dup
在Grails中是否有任何等价物?
我想制作一个域对象的副本。什么是最简单的方法来完成这个?如何在Grails中复制域对象?
我知道我可以创造一个新的记录,然后遍历每个复制数据场逐场域 - 但我想一定有这样做的更简单的方法...
在Rails有一个简单的方法来做到这一点:
#rails < 3.1
new_record = old_record.clone
#rails >= 3.1
new_record = old_record.dup
在Grails中是否有任何等价物?
没有。它已被要求http://jira.grails.org/browse/GRAILS-3532。有人已经为该问题添加了一些代码,但可能会对您有所帮助。
我已经修改了一段代码,可以对域类进行深层克隆。我一直在使用我的系统,它工作得很好(在大多数情况下)。下面的代码是在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
}
回来时,我试图挽救它的对象之前,但。 ..how可以保存这个新对象 – roanjain
你好@roanjain,我会检查这个代码来检查它是否被更新。 但我可以说我成功地使用它来克隆几个类。 – cantoni