views:

328

answers:

3

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.

+1  A: 

Try using: "id( generator: 'assigned')" instead of "id generator: 'identity'" and see if that removes the autoincrement property from the "id" database column.

dave
thanks for the reply. just tested your suggestion now and unfortunately, the 'id' is still there.
firnnauriel
btw, it did removed the autoincrement property of 'id'. but what i need is to totally remove the 'id' column and have 3 columns only: nid, term, weight
firnnauriel
+1  A: 

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

static mapping = {
    version false
    id generator: 'identity', column: 'nid'
}
Luke Daley
looks like this might be another approach. i might need to play with the 'generator' property. please note again that the main goal is to make this statement work w/o any modification to it: insert into snbr_act_vector values (5, 'term', 0.5)
firnnauriel
If that is truly your goal, you may need to do some extra setup in your DB. Perhaps a trigger to translate the statement to the table definition? Otherwise, I think Luke's answer is close to the most you can do from Grails / GORM.
Matt Lachman
+1  A: 

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'
    }
}
Lloyd Meinholz