Grails / GORM属于返回参考命名

时间:2012-07-24 20:08:36

标签: grails gorm belongs-to grails-domain-class

我在Grails中创建了域,专注于我们流程背后的业务逻辑。在某些情况下,这会转换为GORM生成的超出Oracle限制的后向引用,并生成“ORA-00972:标识符太长”错误。我能够使用static mapping块重新映射长表名,但我无法弄清楚如何为生成的后向引用做同样的事情。

在不泄露任何公司机密信息的情况下,以下示例说明了问题。

class UnfortunatelyLongClassName {
    static mapping = {
        table "long_class" // This works great!
    }

    List<Part> parts

    static hasMany = [parts:Part]
}

class Part {
    String name

    // This generates UNFORTUNATELY_LONG_CLASS_NAME_ID and causes the error
    static belongsTo = [UnfortunatelyLongClassName]
}

生成表格的粗略DDL ......

LONG_CLASS (
  ID number(19, 0) not null,
  VERSION number(19, 0) not null,
  primary key (id),
);

PART (
  ID number(19, 0) not null,
  VERSION number(19, 0) not null,
  NAME varchar2(255),
  PARTS_IDX number(10, 0),
  UNFORTUNATELY_LONG_CLASS_NAME_ID number(19, 0) not null,
  primary key (id),
  foreign key FK589895C372DB95A (UNFORTUNATELY_LONG_CLASS_NAME_ID) references UNFORTUNATELY_LONG_CLASS_NAME(ID)
);

是否有任何静态映射命令或其他Grails / GORM技巧来创建更短的标识符?

如果我使用以下内容......

static belongsTo = [unfortunatelyLongClassName:UnfortunatelyLongClassName]

static mapping = {
    unfortunatelyLongClassName column:"ulcn_id"
}

我收到以下错误......

| Error 2012-07-24 17:53:49,060 [pool-7-thread-1] ERROR context.ContextLoader  - Context initialization failed
Message: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.InvalidPropertyException: No property found for name [unfortunatelyLongClassName] for class [class mycompany.myproject.mypackage.Part]
   Line | Method
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run      in     ''
^   662 | run . .  in java.lang.Thread

Caused by InvalidPropertyException: No property found for name [unfortunatelyLongClassName] for class [class mycompany.myproject.mypackage.Part]
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run      in     ''
^   662 | run . .  in java.lang.Thread
| Error 2012-07-24 17:53:49,094 [pool-7-thread-1] ERROR context.GrailsContextLoader  - Error executing bootstraps: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.InvalidPropertyException: No property found for name [unfortunatelyLongClassName] for class [class mycompany.myproject.mypackage.Part]
Message: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.InvalidPropertyException: No property found for name [unfortunatelyLongClassName] for class [class mycompany.myproject.mypackage.Part]
   Line | Method
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run      in     ''
^   662 | run . .  in java.lang.Thread

Caused by InvalidPropertyException: No property found for name [unfortunatelyLongClassName] for class [class mycompany.myproject.mypackage.Part]
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run      in     ''
^   662 | run . .  in java.lang.Thread

2 个答案:

答案 0 :(得分:3)

使用belongsTo中的Map语法生成反向链接并通过映射重命名:

static belongsTo = [unfortunatelyLongClassName:UnfortunatelyLongClassName]

static mapping = {
    unfortunatelyLongClassName column:"ulcn_id"
}

belongsTo也应该使用您的Domain类中的字段,因此您可以这样做:

UnfortunatelyLongClassName unfortunatelyLongClassName

static belongsTo = UnfortunatelyLongClassName

static mapping = {
    unfortunatelyLongClassName column:"ulcn_id"
}

由于之前的版本抛出缺少的属性异常,您可以尝试使用短名称创建属性并跳过映射块:

UnfortunatelyLongClassName ulcn

static belongsTo = UnfortunatelyLongClassName

答案 1 :(得分:0)

如果您尝试重命名长类的ID怎么办?像:

static mapping = {
    id name: 'simple_id_name'
}