8

I'm trying to do this, but I get the error.

"a different object with the same identifier value was already associated with the session"

It looks like I need to remove dbObject from the hibernate session.

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}
}
ScArcher2
  • 85,501
  • 44
  • 121
  • 160

1 Answers1

18

dbObject.discard() did the trick.

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

See the GORM discard() documentation.

Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
ScArcher2
  • 85,501
  • 44
  • 121
  • 160