merge

Purpose

Merges a domain class instance back into the current persistent context.

Examples

def b = new Book(title:"The Shining")
b.merge()

Description

The merge method is similar in function to the save method, but not in behaviour. The merge allows the saving of "detached" instances such as those stored in the HTTP session. Essentially, each persistent instance is associated with a persistence context. A new persistence context is created for each request. The result is objects stored in the session lose their persistent context on subsequent requests. In this case you can't simply call save as the domain class is not associated with a current context.

The merge method on the other hand can be called and its behaviour is defined in terms of the Hibernate documentation:

Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. If the given instance is unsaved, save a copy of and return it as a newly persistent instance.

The merge method is equivalent to the Hibernate merge method.

Parameters: