我正在尝试这样做,但我收到了错误。
“具有相同标识符值的其他对象已与会话相关联”
看起来我需要从hibernate会话中删除dbObject。
def object = messageParserService.parseMessage(messageType, messageText)
def dbObject = object.getClass().findByIdentifier(object.identifier)
if(dbObject != null){
object.id = dbObject.id
object.dateCreated = dbObject.dateCreated
}
if(!object.save()) {
object.errors.each {println it}
}
答案 0 :(得分:16)
dbObject.discard()完成了这个伎俩。
def object = messageParserService.parseMessage(messageType, messageText)
def dbObject = object.getClass().findByIdentifier(object.identifier)
if(dbObject != null){
object.id = dbObject.id
object.dateCreated = dbObject.dateCreated
dbObject.discard()
}
if(!object.save()) {
object.errors.each {println it}
}