(Quick Reference)

attach

Purpose

Attaches a detached domain instance to the Hibernate session bound to the current thread

Examples

def b = Book.get(1)
b.title = "Blah"
b.save(flush:true)

b.discard()

if(!b.isAttached()) { b.attach() }

Description

Grails uses Hibernate which manages each persistent object in a persistence session. A new session is created per request and is closed at the end of the request. If an object is read from the session and placed into a web scope such as the HttpSession it is seen as detached, since the persistence session has been closed. You can use the attach() method to re-attach an existing persistent instance to the persistence session of the current request.