Search code examples
grailsormgrails-ormhibernate-mappinggrails-domain-class

How do I change the name of a Grails domain class id field?


I have a Grails 2.2.3 domain class called FundType that I am trying to map to a legacy database table. It has two fields: code and description. I would like the id to be called code anytime I use the domain class and preferably on any of the generated scaffolding. But every time I use the name key on id I get this exception:

| 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

This is what my domain class consists of:

class FundType {

    String id
    String description

    static mapping = {
        id column: 'fund_code', generator: 'assigned', name: 'code'
        description column: 'fund_desc'
    }
}

And anytime I am using a FundType instance I would like to call code like fundTypeInstance.code and NOT fundTypeInstance.id. This will make it more user friendly for me because I'm dealing with something called code, not id.

So I would like to know is what I'd like to do possible? And what am I doing wrong in my domain class that is causing this ORM mappings error?

Edit:

Okay, so I changed my domain class to the following and I am getting a FundType not found with ID null error.

class FundType {

    String code
    String description

    static mapping = {
        id generator: 'assigned', name: 'code'
        code column: 'fund_code'
        description column: 'fund_desc'
    }
}

I added some sql logging to see what Hibernate is doing and this is what was output: select * from ( select this_.FUND_CODE as RTVFTYP1_1_0_, this_.FUND_DESC as RTVFTYP2_1_0_ from RTVFTYP this_ ) where rownum <= ?


Solution

  • Use String code instead of String id in the domain class.

    You are deliberately mentioning to the GORM that I want to use the property code which maps to table column fund_code whose value is assigned as the id (primary key). In that case, you just need to have the property codedefined in the domain class instead of the id.