如何使用自己的字段创建具有复合id的GORM类?

时间:2013-01-07 09:56:35

标签: gorm grails-2.0

这是我的域类

 class ReturnReason implements Serializable {

                Long returnReasonId
                Long languageId
                String name

                int hashCode() {
                    def builder = new HashCodeBuilder()
                    builder.append returnReasonId
                    builder.append languageId
                    builder.toHashCode()
                }

                boolean equals(other) {
                    if (other == null) return false
                    def builder = new EqualsBuilder()
                    builder.append returnReasonId, other.returnReasonId
                    builder.append languageId, other.languageId
                    builder.isEquals()
                }

                static mapping = {
                    id composite: ["returnReasonId", "languageId"]
                    version false
                }

                static constraints = {
                    name maxSize: 128
                }
            }

这是我的控制器代码,用于保存我的域类。

def save() {
    ReturnReason returnReasonInstance = new ReturnReason(params)
    returnReasonInstance.languageId = 1
    if (!returnReasonInstance.save(flush: true)) {
        render(view: "create", model: [returnReasonInstance: returnReasonInstance])
    }
    redirect(action: "list")
}

尝试保存在我的控制器中而不是returnReasonId中存在错误,即returnReasonId拒绝值null。如何解决它。??

1 个答案:

答案 0 :(得分:0)

在保存操作中写入validate:false

def save() {
    ReturnStatus returnStatusInstance = new ReturnStatus(params)
    returnStatusInstance.languageId = 1
    if (!returnStatusInstance.save(validate: false, flush: true)) {
        render(view: "create", model: [returnStatusInstance: returnStatusInstance])
    }
    redirect(action: "list")
}