Using assigned ID for domain object in Grails 2.0

邮差的信 提交于 2019-12-25 08:48:09

问题


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?


回答1:


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 {
        ...
    }   
}


来源:https://stackoverflow.com/questions/9826509/using-assigned-id-for-domain-object-in-grails-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!