在Grails 2.0中为域对象使用分配的ID [英] Using assigned ID for domain object in Grails 2.0

查看:110
本文介绍了在Grails 2.0中为域对象使用分配的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们已经尝试过: $ b $ 我们正在使用带有遗留数据库的Grails,并且我们需要控制ID如何分配给域对象。 b

  id列:sco_id,发生器:'赋值'

但是我们得到了一个异常:

lockquote

批量更新返回来自update [0]的意外行数;实际行
计数:0;预计:1 / b

我们也尝试创建一个自定义ID生成器:



< pre $ public $ {code $> public class ScoIdGenerator implements IdentifierGenerator {

public Serializable generate(SessionImplementor session,Object object){

/ *在这里生成ID * /

return 8;
}

}

但似乎生成器是在这种情况下被忽略,所以我们得到错误

  DEFAULT关键字不能用作列没有DEFAULT 

code>

我不确定这些问题是否针对Grails 2。



任何帮助表示赞赏?

解决方案

这里的问题是我们试图用列块配置id

  static mapping = {
tabletable_name

列{
id生成器:​​'分配',列:id_sco,sqlType:int
}
}

相反,我们需要直接在静态映射块内部配置id。

  static mapping = {
table table_name

id生成器:​​'assigned',列:id_sco,sqlType:int
列{
...
}
}


We are using Grails with a legacy database and we need to control how ID's get assigned to domain objects.

We have tried:

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

but we get the exception:

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

we have also tried to create a custom ID generator:

public class ScoIdGenerator implements IdentifierGenerator {

    public Serializable generate(SessionImplementor session, Object object) {

        /*Generate ID here*/

        return 8;
    }

}

But it seems like the generator is being ignored in this case so we get the error

DEFAULT keyword cannot be used as column has no DEFAULT

I am not sure if these issues are specific to Grails 2.

Any help appreciated?

解决方案

The issue here was that we were attempting to configure the id with the columns block

static mapping = {
    table "table_name"

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

Instead we needed to configure the id directly inside the static mapping block

static mapping = {
    table "table_name"

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

这篇关于在Grails 2.0中为域对象使用分配的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆