Skip to content

Instantly share code, notes, and snippets.

@mathifonseca
Last active July 15, 2019 00:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathifonseca/507593708b02c56b10da to your computer and use it in GitHub Desktop.
Save mathifonseca/507593708b02c56b10da to your computer and use it in GitHub Desktop.
GRAILS - Assigned id for domains

If you have a Domain class with an assigned id (not the default autoincremented long), the id assignment must not be in the constructor, but in a different line.

That means, having this domain:

class State implements Serializable {
    String id
    static mapping = {
    	id column: 'id', generator: 'assigned'
    }
}

This line doesn't work:

def am = new State(id: 'AM', name: 'Amazonas').save()

But although they seem the same, these three do work:

def am = new State(name: 'Amazonas')
am.id = 'AM'
am.save()

Here are some explanations from the Grails team:

@jamesdh
Copy link

jamesdh commented Jul 3, 2019

Stumbled upon this and just wanted to let anyone else who might do the same know that this was fixed with the bindable constraint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment