Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you disconnect an object from it's hibernate session in grails?

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}
}
like image 356
ScArcher2 Avatar asked Feb 18 '10 16:02

ScArcher2


1 Answers

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.

like image 185
ScArcher2 Avatar answered Sep 20 '22 07:09

ScArcher2