grails - 在Grails 2.0中为域对象使用分配的ID

标签 grails gorm grails-2.0

我们正在将Grails与旧版数据库一起使用,我们需要控制如何将ID分配给域对象。

我们尝试过:

id column: "sco_id", generator:'assigned'

但我们得到了异常(exception):

Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1



我们还尝试创建一个自定义ID生成器:
public class ScoIdGenerator implements IdentifierGenerator {

    public Serializable generate(SessionImplementor session, Object object) {

        /*Generate ID here*/

        return 8;
    }

}

但似乎在这种情况下发电机被忽略了,所以我们得到了错误
DEFAULT keyword cannot be used as column has no DEFAULT

我不确定这些问题是否特定于Grails 2。

任何帮助表示赞赏吗?

最佳答案

这里的问题是我们正在尝试使用column块配置id

static mapping = {
    table "table_name"

    columns {
        id generator: 'assigned', column: "id_sco", sqlType: "int"
    }   
}

相反,我们需要直接在静态映射块内配置id
static mapping = {
    table "table_name"

    id generator: 'assigned', column: "id_sco", sqlType: "int"
    columns {
        ...
    }   
}

关于grails - 在Grails 2.0中为域对象使用分配的ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9826509/

相关文章:

grails - 如何将 Grails 2.1 迁移到 Grails 2.3 应用程序?

Grails:是否可以防止域类实例被持久化?

grails - 为什么Grails似乎正在保存无效的域对象?

grails - 在grails中没有hibernate property-ref等效项

grails - 如何创建自定义Grails查询

grails - Grails:findAllByTitle()作为搜索功能?

grails - 具有hasMany的Grails类

注销后 Grails Spring 安全重定向

grails - Grails Quartz插件并发不起作用

grails - 在Grails 2.3.4中使用记录器