Grails / GORM: Changing default id name and type from an entity

October 4, 2011

While developing a simple application at work I got stuck with a common case in GORM, and I didn’t found the solution in the documentation.

I have a table with a column named myid of type varchar. But GORM by default auto generate an id property with the long type. The solution is straight forward, just call the method id in the mapping closure, with the following param(s):

class Foo {

    String myid

    static constraints = {}
    static mapping = {
        id generator: 'assigned', name: 'myid'

The generator is the strategy to auto-generate the id, you can use a sequence or something like, since it’s a string the value will be assigned, not generated. The name is the current property to bind the value (myid in this case).

comments powered by Disqus