Wednesday, October 20, 2010

Grails GORM trick: Use read() rather than get() on updates

Hi

So i discovered that it might be a good idea use read() on your update method.

So use

def person = Person.read(params.id)

instead of

def person = Person.get(params.id)

The consequence of this is that hibernate dirty checking is turned off. So to save your domain object you MUST call save(). So if you cancel the update for any reason without calling save() you do not have to to worry about grails persisting things automatically due to dirty checking :)

Hope this helps

2 comments:

Blundell said...

Just figured this one myself, your blog posts confirms I can do it ;-) thanks

Bil! said...

Any way to do a similar thing with dynamic finders (findAll, etc)?