0
votes

There is a domain class with field id being injected by Grails. Instantiating such a class with a map results in id == null:

def obj = DomainClass.class.newInstance(id:100, name:'Joe')
assert obj.id != null // it fails

What is the best practice to assign all the hidden properties in Grails?

1

1 Answers

1
votes

You can customize how GORM generates identifiers for the database using the DSL. By default GORM relies on the native database mechanism for generating ids. Check the documentation to customizes the way the identifier for a domain class is generated.

static mapping = {
    id generator: 'assigned'
}

There is a bug over id initialization on constructor when generator is assigned, take a look to this blog post with a temporary workaround for this bug: Assigning ID for domain objects in Grails via constructor.