Using assigned ID for domain object in Grails 2.0

2019-08-28 04:30发布

问题:

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