Showing posts with label groovy. Show all posts
Showing posts with label groovy. Show all posts

Thursday, March 24, 2011

manually set id of grails domain object

Problem:
if you create a new domain object and set its id in the constructor like this:
Cat catman = new Cat(id:1000, name: "catman", kind: "domestic short haired")
later, if you look at the object the id will be null. what gives?!


Solution:
Its madness, but it works to assign the id after the object is made. Just add
catman.setId(1000);
and you are in business.

Note: if you want to save the object into db after doing this, you must set the id generator to 'assigned'

Saturday, December 04, 2010

Convert map to object in groovy

You need to convert a map into an object in groovy. There must be a better way then iterating through the map and setting all the properties in the object via reflection or otherwise. There is: