6.3.1 Saving and Updating - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari
Version: 3.0.11
6.3.1 Saving and Updating
An example of using the save method can be seen below:def p = Person.get(1) p.save()
def p = Person.get(1)
p.save(flush: true)
def p = Person.get(1) try { p.save(flush: true) } catch (org.springframework.dao.DataIntegrityViolationException e) { // deal with exception }
save()
will simply return null
in this case, but if you would prefer it to throw an exception you can use the failOnError
argument:def p = Person.get(1) try { p.save(failOnError: true) } catch (ValidationException e) { // deal with exception }
application.groovy
, as described in the section on configuration. Just remember that when you are saving domain instances that have been bound with data provided by the user, the likelihood of validation exceptions is quite high and you won't want those exceptions propagating to the end user.You can find out more about the subtleties of saving data in this article - a must read!