grails - Grails/GORM属于返回引用命名

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

我在Grails中创建了域,以专注于流程背后的业务逻辑。在某些情况下,这会转换为GORM生成的反向引用超出了Oracle的限制,并生成了“ORA-00972:标识符太长”错误。我可以使用static mapping块来重新映射长表名,但是我不知道如何对生成的反向引用执行相同的操作。

在不公开任何公司 secret 信息的情况下,以下示例说明了该问题。

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

最佳答案

使用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

关于grails - Grails/GORM属于返回引用命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11638549/

相关文章:

ruby-on-rails - Ruby on Rails - 具有 belongs_to 关联的表单

grails - 在Grails标记库中使用<<

jquery - 如何通过geb测试select2

grails - 提升与 Grails 的比较

grails - GRAILS GORM:渴望级联风格

ruby-on-rails - Rails has_many belongs_to with foreign_key 和 class_name

grails - 使用 spring-security-core 插件将 Facebook 登录 (oauth) 与现有的 Grails 应用程序集成

hibernate - Grails条件:id不在hasMany列表中

grails - GORM没有保存我的hasMany实体

ruby-on-rails - 如果数据库中没有外键,Rails关联是否应该引发错误?