Disable the natively generated identity value feature in grails

风流意气都作罢 提交于 2020-01-16 23:14:02

问题


The following is my Domain class details.

class Age {
    String agetype
    static constraints = {
    }

}

I am using HeidiSQL. I want to drop the id column that is generated automatically.And set primary key as 'agetype'. what I can I do?


回答1:


Identifier can be customized easily inside the mapping block, if the default id is not required.

class Age {
    String agetype

    static mapping = {
        id name: 'agetype', 
           column: 'AGE_TYPE', // if the column name is AGE_TYPE
           generator: 'assigned' // Unique String should assigned for agetype
    }

    static constraints = {
        agetype bindable: true //identifiers are not bindable by default
    }
}

With the above setup, you should be able to create Age as:

new Age(agetype: 'Teen').save(flush: true)

Above will through a primary key violation if run again.

Refer docs for more details about customizing id and column as required.



来源:https://stackoverflow.com/questions/23622847/disable-the-natively-generated-identity-value-feature-in-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!