grails3.8版本 联合主键 创建 及可能出现的问题

建立domain

class Test implements Serializable{

String tes

Date insertDate

static constraints = {

  tes(nullable:false)

  insertDate(nullable:false)
}
static mapping = {

//联合主键创建
id composite:['tes', 'insertDate']

}

}

若启动失败 。加入以下语句

boolean equals(other) {
if (!(other instanceof Test)) {
return false
}

other.tes == tes && other.insertDate == insertDate
}

int hashCode() {
def builder = new HashCodeBuilder()
builder.append tes
builder.append insertDate
builder.toHashCode()
}

使用复合主键映射的域类必须使用复合键中的属性来实现Serializable接口并覆盖equalshashCode方法,以进行计算。上面的例子使用了一个HashCodeBuilder方便,但自己实现它是好的。

可参考:http://gorm.grails.org/latest/hibernate/manual/index.html

posted @ 2019-06-17 16:16  易行舟  阅读(302)  评论(0)    收藏  举报