简体   繁体   English

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

[英]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. 我有一个名为FundType的Grails 2.2.3域类,我试图映射到遗留数据库表。 It has two fields: code and description . 它有两个字段: codedescription I would like the id to be called code anytime I use the domain class and preferably on any of the generated scaffolding. 我想在我使用域类的时候将id称为code ,最好是在任何生成的脚手架上。 But every time I use the name key on id I get this exception: 但是每次我在id上使用name键时我都会遇到这个异常:

| 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 . 每当我使用FundType实例时,我都想调用像fundTypeInstance.code和NOT fundTypeInstance.id这样的代码。 This will make it more user friendly for me because I'm dealing with something called code, not id. 这将使我对用户更友好,因为我正在处理一些叫做代码的东西,而不是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? 我的域类中导致此ORM映射错误的错误是什么?

Edit: 编辑:

Okay, so I changed my domain class to the following and I am getting a FundType not found with ID null error. 好的,所以我将我的域类更改为以下内容,并且我找到了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'
    }
}

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

Use String code instead of String id in the domain class. 在域类中使用String code而不是String id

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). 您有fund_code GORM提及我要使用映射到表列fund_code的属性code ,其值被指定为id (主键)。 In that case, you just need to have the property code defined in the domain class instead of the id . 在这种情况下,您只需要在域类中定义属性code而不是id

(I'm answering the fix that worked for me for future use by other programmers) (我正在回答有助于我将来使用的其他程序员的修复)

@dmahapatro was right, I needed to add String code . @dmahapatro是对的,我需要添加String code

It looks like naming the id something different just doesn't play well with Grails dynamic scaffolding. 它看起来像命名id不同的东西只是不能很好地与Grails动态脚手架。 I did some tests and I can still use FundType.get(code) and it will return the object just as if I passed in an id. 我做了一些测试,我仍然可以使用FundType.get(code) ,它将返回对象就像我传入一个id一样。 I can also do FundType.findByCode(code) . 我也可以做FundType.findByCode(code)

It looks like I have to change the scaffolded controller to expect a String id instead of the default Long id. 看起来我必须更改scaffolded控制器以期望String id而不是默认的Long id。 I also have to change the scaffolded list view to send fundTypeInstance.code instead of fundTypeInstance.id to the show controller, but I suspect that adding a getId() that just returns this.code will fix that. 我还必须更改scaffolded list视图以将fundTypeInstance.code而不是fundTypeInstance.id发送到show controller,但我怀疑添加一个只返回this.code的getId()将解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM