我有 GORM 域类,它在静态映射中定义了一个复合键,如下所示:
static mapping = {
table 'myTable'
id composite: ['Y', 'Z']
X column: 'xColName'
Y column: 'yColName'
}
我需要更改此类以删除复合主键并将主键设置为具有自动递增值的通用 id 字段。但是,表“myTable”中当前存在行,当我从映射中删除“复合”行时,Grails 添加默认的“id”,但“id”的默认值为每一行的“0”。
| X | Y | id |
==========================
| a | d | 0 |
| b | e | 0 |
| c | f | 0 |
我尝试了许多不同的“生成器”以及下面 id 列的所有类型的组合:
static mapping = {
id column: 'id', generator: 'increment'
...
}
我还运行了生成以下内容的 dbm-gorm-diff:
changeSet(author: " (generated)", id: "1434577127626-10") {
dropPrimaryKey(tableName: "myTable")
addPrimaryKey(columnNames: "id", constraintName: "classNamePK", tableName: "myTable")
}
但是,当变更日志运行时,GORM 已经将我的域对象加载到内存中,并在 id 字段中添加了全为“0”的值。有没有办法让 myTable 将 id 列设置为主键并生成序列号?还是我需要自己添加这些然后更改changelog.groovy中的主键来跳过一些障碍
| X | Y | id |
==========================
| a | d | 1 |
| b | e | 2 |
| c | f | 3 |
最佳答案
如果您使用的是 Oracle 数据源,您可以在数据库中创建一个序列并指定使用它:
static mapping = {
id generator: 'sequence', params: [sequence: 'MY_SEQUENCE']
}
查看Grails documentation有关生成器的更多信息,和/或它链接到的 Hibernate 引用页面。
关于grails - GORM : Change from composite key to auto incrementing primary key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30904328/