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

后端 未结 5 1944
孤城傲影
孤城傲影 2021-02-06 05:32

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

    st         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 05:44

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

提交回复
热议问题