Search code examples
hibernategrailsgrails-orm

When to save an instance in grails?


What are the guidelines to save the instance in grails?

I am doing something like this:

def colorInstance = Color.findOrSaveByName(colorname)
if (colorInstance.startsWith("R")) {
   colorInstance.isRColor = true;
}

Should I be calling colorInstance.save() in the if block or not? and why?


Solution

  • You can save an instance explicitly (just as per your example) whenever your flow requires it.

    Consider that at the end of a request cycle, when a GORM session is flushed, a dirty check operation is performed. Objects that are binded to the session are compared against their original values and if changes are detected, they are persisted.

    From http://www.redwindsw.com/blog/2014-01-15-moving-from-rails-to-grails-differences-and-similarities, read the paragraph about The Hibernate Session.