grails - 为什么Grails不使用提供的联接表名称(多对多关系)

标签 grails gorm

背景

我正在评估Grails,因此必须为旧数据库映射持久层。我从三个表开始:

  • Clone
  • 和其他属性:主键id
  • CloneSet
  • 和其他属性:主键id
  • Clone2CloneSet
  • 只是两个外键cloneIDcloneSetID

  • 域类的编码如下:

    class Clone {
        // among others
        static hasMany = [cloneSets: CloneSet]
    
        static mapping = {
            id (generator: 'identity')
            cloneSets (
                joinTable: [name: 'Clone2CloneSet', key: 'cloneID', column: 'cloneSetID'],
                cascade: 'none'
            )
        }
    }
    
    class CloneSet {
        // among others
        static hasMany = [clones: Clone]
        static belongsTo = Clone
        static mappedBy = [clones: "cloneSets"]
    
        static mapping = {
            table (name: 'CloneSet') 
            id (generator: 'identity')
            clones (
                joinTable: [name: 'Clone2CloneSet', key: 'cloneSetID', column: 'cloneID'],
                cascade: 'none'
            )
        }
    }
    

    问题

    Grails似乎坚持认为我的联接表的名称是clone2clone_set:
    2013-09-12 10:39:26,459 [localhost-startStop-1] INFO  hbm2ddl.TableMetadata  - table found: mydatabase.dbo.CloneSet
    2013-09-12 10:39:26,459 [localhost-startStop-1] INFO  hbm2ddl.TableMetadata  - columns: [id, ...]
    2013-09-12 10:39:26,465 [localhost-startStop-1] INFO  hbm2ddl.TableMetadata  - table found: mydatabase.dbo.Clone
    2013-09-12 10:39:26,465 [localhost-startStop-1] INFO  hbm2ddl.TableMetadata  - columns: [id, ...]
    2013-09-12 10:39:26,469 [localhost-startStop-1] INFO  hbm2ddl.DatabaseMetadata  - table not found: clone2clone_set
    | Error 2013-09-12 10:39:26,481 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing table: clone2clone_set
    Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing table: clone2clone_set
        Line | Method
    ->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    |    166 | run       in java.util.concurrent.FutureTask
    |   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
    |    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
    ^    724 | run . . . in java.lang.Thread
    Caused by BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing table: clone2clone_set
    ->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    |    166 | run       in java.util.concurrent.FutureTask
    |   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
    |    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
    ^    724 | run . . . in java.lang.Thread
    Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing table: clone2clone_set
    ->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    |    166 | run       in java.util.concurrent.FutureTask
    |   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
    |    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
    ^    724 | run . . . in java.lang.Thread
    Caused by HibernateException: Missing table: clone2clone_set
    ->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    |    166 | run       in java.util.concurrent.FutureTask
    |   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
    |    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
    ^    724 | run . . . in java.lang.Thread
    



    如何说服Grails搜索正确的联接表?

    最佳答案

    我想您不需要mappedBy中的CloneSet

    class CloneSet {
    
        static hasMany = [clones: Clone]
        static belongsTo = Clone
    
        //Do you really need this?
        //This maps to CloneSet which is incorrect 
        //static mappedBy = [clones: "cloneSets"]
    
        static mapping = {
            table name: 'CloneSet'
            id generator: 'identity'
            clones joinTable: [name: 'Clone2CloneSet', key: 'cloneSetID', 
                               column: 'cloneID'],
                   cascade: 'none'
    
        }
    }
    

    此外,如果可行,表名称可以变为CLONE_SET,CLONE_CLONE_SET(或CLONE_CLONE_SET_XREF作为交叉引用)。 Grails使用CamelCase作为域名,并将表命名为Camel_Case

    关于grails - 为什么Grails不使用提供的联接表名称(多对多关系),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18760057/

    相关文章:

    javascript - g :message with arguments inside Javascript/jQuery not working as expected

    grails - 克 :if inside g:link does not work

    ajax - Grails-通过单个remoteFunction传递多个AJAX参数?

    grails - 用grails控制台创建的新域对象在dbconsole中不可见

    grails - GORM不会在两个类之间的一对多关系中创建表

    grails - GORM-将3个查询更改为1个(或至少2个)

    hibernate - 约束条件查询以获取笛卡尔积并返回结果作为对象数组列表

    grails - 在Grails应用程序中使用rest api并在gsp页面中打印响应

    groovy - Grails 1.2.x脚本不起作用

    postgresql - 带有like子句和foreign-key/belongs-to子句的GORM查询出现问题