gpt4 book ai didi

hibernate - 在grails中获取独立的域实体

转载 作者:行者123 更新时间:2023-12-02 13:51:45 26 4
gpt4 key购买 nike

当同一方法中存在另一个具有相同ID的域实体时,我想获得处于分离状态的grails中的域实体。

我遵循此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 session 呢?

最佳答案

discard不返回实例本身。它不返回任何值(无效),但逐出该对象在将来变得持久。用作:

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]
]
}

关于hibernate - 在grails中获取独立的域实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23600922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com