2014-05-12 2 views
2

같은 방법으로 동일한 id에 대해 다른 도메인 엔티티가있을 때 분리 된 상태의 grails에서 도메인 엔티티를 가져 오려고합니다.grails에서 detached domain entity 얻기

다음은이 글 How do you disconnect an object from it's hibernate session in grails?을 grails에서 분리 된 도메인 엔티티를 얻는 방법으로 따랐습니다. 그래서

def id = 23L; 
def userInstance = User.get(id) 
def oldInstance = User.get(id).discard() 

userInstance.properties = params 

userInstace.save(flush:true) 

// Now, I want to compare properties of oldInstance and userInstance 
// But I get null for oldInstance 

, 어떻게 그것이 GORM 세션에서 분리되도록 세부 Grails의에서 도메인 개체를 얻을 수 있나요?

답변

4

discard 인스턴스 자체를 반환하지 않습니다. 아무것도 반환하지 않고 (void) 미래에 지속되는 객체를 제거합니다. 유일한 이유는 인스턴스에서 속성의 이전 및 새 값을 비교하는 경우

보조 노트에
def oldInstance = User.get(id) 
oldInstance.discard() 

, 당신은 다음과 같이 인스턴스를 세척하기 전에 dirtyPropertyNamesgetPersistentValue()를 사용할 수 있습니다로 사용 호출 한 후

userInstance.properties = params 

userInstance.dirtyPropertyNames?.each { name -> 
    def originalValue = userInstance.getPersistentValue(name) 
    def newValue = userInstance.name 
} 

//Or groovier way 
userInstance.dirtyPropertyNames?.collect { 
    [ 
     (it) : [oldValue: userInstance.getPersistentValue(it), 
       newValue: userInstance.it] 
    ] 
} 
+0

는 윌'userInstance.dirtyPropertyNames' 일'userInstance.save()' – TheKojuEffect

+0

없음이 저장 전에 사용하는 없습니다. 저장된 후에는 더티 속성이 모두 새 값으로 플러시됩니다. 이전 값이 손실됩니다. – dmahapatro

+0

감사. Intellij를 사용하고 있는데'dirtyPropertyNames'와'userInstance.getPersistentValue'에서'can not resolve symbol'을 받았습니다. 어떤 생각? – TheKojuEffect

관련 문제