Is it possible to explicitly set the id of a domain object in Grails’ Bootstrap.groovy (or anywhere, for that matter)?
I’ve tried the following:
new Foo(id: 1234, name: 'My Foo').save()
and:
def foo = new Foo() foo.id = 1234 foo.name = 'My Foo' foo.save()
But in both cases, when I print out the results of Foo.list() at runtime, I see that my object has been given an id of 1, or whatever the next id in the sequence is.
Edit: This is in Grails 1.0.3, and when I’m running my application in ‘dev’ with the built-in HSQL database.
Edit: chanwit has provided one good solution below. However, I was actually looking for a way to set the id without changing my domain’s id generation method. This is primarily for testing: I’d like to be able to set certain things to known id values either in my test bootstrap or setUp(), but still be able to use auto_increment or a sequence in production.
Yes, with manually GORM mapping:
and your second snippet (not the first one) will do the job (Id won’t be assigned when passing it through constructor).