我正在评估Grails,因此必须映射遗留数据库的持久层。我从三张桌子开始:
Clone
id
CloneSet
id
Clone2CloneSet
cloneID
和cloneSetID
域类编码如下:
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搜索正确的连接表?
答案 0 :(得分:1)
我想您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
。
答案 1 :(得分:0)
在我的情况下,它将表名设置为clone2cloneset
,因为SQL Server不区分大小写。 (这也必须对所有列名进行处理:没有驼峰式表示法。)