gpt4 book ai didi

Grails 独特的复合键问题

转载 作者:行者123 更新时间:2023-12-02 14:50:40 25 4
gpt4 key购买 nike

给定一个 GORM 类:

    class PriceSheet {

Client client
Population population
Product product
RevenueModelType modelType

BigDecimal price

static constraints = {
client(unique: ['population', 'product', 'modelType'])
}
}

我希望仅在客户、人口、产品和模型类型唯一时才保存/更新 PriceSheet。 (客户、人口、产品和模型类型的组合应该只有一个价格表项)。

key 是在 mySQL 中创建的。

我的问题是 grails 验证通过,但保存失败。
    priceSheetInstance.validate()

if (priceSheetInstance.hasErrors()) {
respond priceSheetInstance.errors, view:'create'
return
}

priceSheetInstance.save flush:true

有什么想法或建议吗?我在验证后将调试器放在断点上,发现错误为空。

chalice 2.3.10

最佳答案

首先,您需要告诉 GORM 哪些属性构成了您的复合主键,然后您的域类需要实现 Serializable。最终结果是这样的:

import org.apache.commons.lang.builder.HashCodeBuilder

class PriceSheet implements Serializable {
Client client
Population population
Product product
RevenueModelType modelType

BigDecimal price

static constraints = {
}

static mapping = {
id composite: ['client', 'population', 'product', 'modelType']
}

boolean equals(other) {
if(!(other instanceof PriceSheet)) return false
other.client == client && other.population == population && other.product == product && other.modelType == modelType
}

int hashCode() {
def builder = new HashCodeBuilder()

builder.with {
append client
append population
append product
append modelType
}

builder.toHashCode()
}
}

您可以阅读有关 GORM 复合键的更多信息 here .

关于Grails 独特的复合键问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31816736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com