【问题标题】:Using assigned ID for domain object in Grails 2.0在 Grails 2.0 中为域对象使用分配的 ID
【发布时间】:2012-03-22 16:28:08
【问题描述】:

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

我们已经尝试过:

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

但我们得到了例外:

批量更新从更新 [0] 返回了意外的行数;实际行 计数:0;预计: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。

任何帮助表示赞赏?

【问题讨论】:

    标签: grails grails-orm grails-2.0


    【解决方案1】:

    这里的问题是我们试图用列块配置 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 {
            ...
        }   
    }
    

    【讨论】:

    • 感谢您发布详细的答案。有一天,它可能会帮助遇到与您相同问题的人!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 2015-01-06
    • 1970-01-01
    相关资源
    最近更新 更多