如何将我的computerId设置为主键?

时间:2016-01-27 10:13:40

标签: grails gorm

这是我在域中的代码。我想将我的computerId设置为主键。但仍然显示在我的桌子上(索引)。感谢

package com.data

class ComputerInformation {

    String computerId;
    String computerName;
    String status;
    String location;
    String serial;
    String monitorSerial;
    String keyboardSerial;
    String mouseSerial;
    String cpuSerial;
    String avrSerial;
    String harddiskSerial;

    static constraints = {
        computerId(unique:true)
        computerName(blank:false)
        status(blank:false)
        location(blank:false)
        serial(blank:false)
        monitorSerial(blank:false)
        keyboardSerial(blank:false)
        mouseSerial(blank:false)
        cpuSerial(blank:false)
        avrSerial(blank:false)
        harddiskSerial(blank:false)
    }
}

2 个答案:

答案 0 :(得分:0)

像这样使用,

static mapping = {
        id name: 'computerId'
}

答案 1 :(得分:0)

也许不是改变PK,而是将id作为computerId变量返回?

package com.data

class ComputerInformation {

    String computerName;
    String status;
    String location;
    String serial;
    String monitorSerial;
    String keyboardSerial;
    String mouseSerial;
    String cpuSerial;
    String avrSerial;
    String harddiskSerial;

    static constraints = {
        computerName(blank:false)
        status(blank:false)
        location(blank:false)
        serial(blank:false)
        monitorSerial(blank:false)
        keyboardSerial(blank:false)
        mouseSerial(blank:false)
        cpuSerial(blank:false)
        avrSerial(blank:false)
        harddiskSerial(blank:false)
    }

    def getComputerId(){
        return id
    }
}

此外,如果您需要将computerId作为String,则可以将getComputerId函数更改为:

String getComputerId(){
        return id.toString()
}