0

背景

我正在评估 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 搜索正确的连接表?

4

2 回答 2

1

我想你不需要mappedByCloneSet.

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用于交叉引用)。GrailsCamelCase用于域名和表的名称为Camel_Case.

于 2013-09-12T13:34:23.133 回答
0

在我的情况下,它可以将表名设置为clone2cloneset,因为 SQL Server 不区分大小写。(这也必须对所有列名进行:那里没有驼峰式表示法。)

于 2013-09-24T15:16:19.197 回答