如何更改Grails域类ID字段的名称?

gra*_*hey 3 orm grails grails-orm hibernate-mapping grails-domain-class

我有一个Grails 2.2.3域类FundType,我试图映射到遗留数据库表.它有两个字段:codedescription.我想在任何时候使用域类并且最好在任何生成的脚手架上id调用code.但每次我使用名称键时,id我都会遇到此异常:

| Error 2013-07-24 09:38:44,855 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: Error evaluating ORM mappings block for domain [com.company.scholallow.FundType]:  null
Message: Error evaluating ORM mappings block for domain [com.company.scholallow.FundType]:  null
Run Code Online (Sandbox Code Playgroud)

这是我的域类包含的内容:

class FundType {

    String id
    String description

    static mapping = {
        id column: 'fund_code', generator: 'assigned', name: 'code'
        description column: 'fund_desc'
    }
}
Run Code Online (Sandbox Code Playgroud)

每当我使用FundType实例时,我都想调用代码fundTypeInstance.code和NOT fundTypeInstance.id.这将使我对用户更友好,因为我正在处理一些叫做代码的东西,而不是id.

所以我想知道我想做的事情是什么?我的域类中导致此ORM映射错误的错误是什么?

编辑:

好的,所以我将我的域类更改为以下内容,并且我找到了ID null null错误的FundType.

class FundType {

    String code
    String description

    static mapping = {
        id generator: 'assigned', name: 'code'
        code column: 'fund_code'
        description column: 'fund_desc'
    }
}
Run Code Online (Sandbox Code Playgroud)

我添加了一些sql日志记录来查看Hibernate正在做什么,这就是输出: select * from ( select this_.FUND_CODE as RTVFTYP1_1_0_, this_.FUND_DESC as RTVFTYP2_1_0_ from RTVFTYP this_ ) where rownum <= ?

dma*_*tro 6

使用String code,而不是String id在域类.

您有意向GORM提及我要使用code映射到表列的属性,fund_code其值被指定为id(主键).在这种情况下,您只需要code在域类中定义属性而不是id.