grails - GORM : Change from composite key to auto incrementing primary key

标签 grails grails-orm

我有 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/

相关文章:

unit-testing - Grails 的 mockDomain() 方法有哪些限制?

grails - 我可以在单独的 Java 项目中创建域类并在 Grails 中导入吗

grails - 检索Grails复合ID属性的列表?

grails - Grails应用程序中的多语言布局

mysql - 如何在 list.gsp 中显示链接表值而不是 id? chalice

rest - 在 grails 中将 transient 域类属性作为 restful json 或 xml 响应包括在内

更新 : Validation error whilst flushing entity on AbstractPersistenceEventListener 上的 ValidationException

hibernate - Grails:生成自定义 ID

grails - 更改 [hasMany :] reference to a list?

grails - 女生中的xml markupbuilder,将属性值中的单引号更改为“