Print Shortlink

Few useful utilities in GORM

You will always learn something new, whenever you work with Grails. It doesn’t fail to surprise you everytime. Last week’s client visit made me sit up and notice few more useful methods in GORM.

failOnError:true.An useful property when applied while saving/updating an domain instance gives you the error messages in the console. Say you have a Person class that you want to instantiate and save to the database. You can write

p1 = new Person(name:"Sam")
p1.save(failOnError:true)

addToXXX method.An useful method to add collections in a domain class. Say Person class has a dogs collection and you can add Dog objects by using addToDogs method like shown below.

	p1 = new Person(name:"Sam")
	p1.addToDogs(new Dog(name:"Snoopy"))
	p1.save(failOnError:true)

These two options though very simple come in very handy in applications

Leave a Reply