Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for a Grails Domain to have no 'id'?

Is it possible to create a table that has no 'id'? For example, this is my domain:

class SnbrActVector {

    int nid
    String term
    double weight

    static mapping = {
        version false
        id generator: 'identity'
    }

    static constraints = {
    }
}

When I run this SQL statement, it fails:

insert into snbr_act_vector values (5, 'term', 0.5)

I checked the table and 'id' is already set to autoincrement. I'm thinking that another option is to remove the 'id' itself. Or is there another workaround for this? Please assume that it is not an option to change the givent SQL statement.

like image 836
firnnauriel Avatar asked Apr 06 '10 05:04

firnnauriel


3 Answers

Gorm requires an id field to work. You can fake an assigned id by using a transient variable like below. The getters and setters map the nid field to the id field.

When saving a domain object using this method you have to do:

snbrActVectgor.save(insert:true)

because grails thinks a non-null id is a persistent instance.

class SnbrActVector {
    Integer id
    // nid is the actual primary key
    static transients = ['nid']
    void setNid(Integer nid) {
        id = nid
    }
    Integer getNid() {
        return nid
    }

    static mapping = {
        version false
        id generator:'assigned', column:'nid', type:'integer'
    }
}
like image 150
Lloyd Meinholz Avatar answered Nov 19 '22 03:11

Lloyd Meinholz


You probably need to specify that nid is your id column.

static mapping = {
    version false
    id generator: 'identity', column: 'nid'
}
like image 44
Luke Daley Avatar answered Nov 19 '22 03:11

Luke Daley


There is no way to have no "id". what you can do is change the name of "id" field using assigned id generator.

like image 2
Viral Thakkar Avatar answered Nov 19 '22 03:11

Viral Thakkar