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

后端 未结 5 1878
孤城傲影
孤城傲影 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'
        }
    }
    
    0 讨论(0)
  • 2021-02-06 05:45

    Yes in Oracle you can use ROWID for ID column.

    class Document {
        String id
    
        static mapping = {
            table "DOCUMENTS"
            version false
            id column: 'ROWID'
        }
    
    }
    
    0 讨论(0)
  • 2021-02-06 05:46

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

    0 讨论(0)
  • 2021-02-06 05:50

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

    static mapping = {
        version false
        id generator: 'identity', column: 'nid'
    }
    
    0 讨论(0)
  • 2021-02-06 05:52

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

    0 讨论(0)
提交回复
热议问题