gpt4 book ai didi

Grails 列默认值未默认

转载 作者:行者123 更新时间:2023-12-04 18:04:44 25 4
gpt4 key购买 nike

我正在使用 Grails 2.4.3 并且有这个域类:

class StockItem extends DisplayableDomain {

String name
Integer quantityOnHand
BigDecimal wholesalePrice
BigDecimal retailPrice
BigDecimal profit

static constraints = {
name minSize: 3, maxSize: 80
wholesalePrice min: 0.0, scale: 2
retailPrice min: 0.0, scale: 2, validator: { retailPrice, StockItem obj ->
if (retailPrice < obj.wholesalePrice) {
['retailLessThanWholesale']
}
}
quantityOnHand min: 0
profit nullable: true
}

@Override
String getDisplayString() {
name
}

static mapping = {
profit formula: "RETAIL_PRICE - WHOLESALE_PRICE"
quantityOnHand column: 'quantityOnHand', defaultValue: "0"
}
}
当我尝试添加 StockItem 时,我收到此错误:
Message: Validation Error(s) occurred during save():
- Field error in object 'com.waldoware.invoicer.StockItem' on field 'quantityOnHand': rejected value [null]; codes [com.waldoware.invoicer.StockItem.quantityOnHand.nullable.error.com.waldoware.invoicer.StockItem.quantityOnHand,com.waldoware.invoicer.StockItem.quantityOnHand.nullable.error.quantityOnHand,com.waldoware.invoicer.StockItem.quantityOnHand.nullable.error.java.lang.Integer,com.waldoware.invoicer.StockItem.quantityOnHand.nullable.error,stockItem.quantityOnHand.nullable.error.com.waldoware.invoicer.StockItem.quantityOnHand,stockItem.quantityOnHand.nullable.error.quantityOnHand,stockItem.quantityOnHand.nullable.error.java.lang.Integer,stockItem.quantityOnHand.nullable.error,com.waldoware.invoicer.StockItem.quantityOnHand.nullable.com.waldoware.invoicer.StockItem.quantityOnHand,com.waldoware.invoicer.StockItem.quantityOnHand.nullable.quantityOnHand,com.waldoware.invoicer.StockItem.quantityOnHand.nullable.java.lang.Integer,com.waldoware.invoicer.StockItem.quantityOnHand.nullable,stockItem.quantityOnHand.nullable.com.waldoware.invoicer.StockItem.quantityOnHand,stockItem.quantityOnHand.nullable.quantityOnHand,stockItem.quantityOnHand.nullable.java.lang.Integer,stockItem.quantityOnHand.nullable,nullable.com.waldoware.invoicer.StockItem.quantityOnHand,nullable.quantityOnHand,nullable.java.lang.Integer,nullable]; arguments [quantityOnHand,class com.waldoware.invoicer.StockItem]; default message [Property [{0}] of class [{1}] cannot be null]
显然是 quantityOnHand 的默认值没有设置。我试过将默认值放在引号中以及一个独立的整数值中。我也试过设置 quantityOnHandnullable .这可以防止错误,但该列为空。

最佳答案

正如@Biswas 所评论的,属性定义中的简单赋值解决了默认值的问题。

class StockItem extends DisplayableDomain {
Integer quantityOnHand = 0
}
此外,在 OP 的情况下,使用 int会解决问题。包装类,如 Integer , DoubleBoolean将它们的值设置为 null默认情况下,尽管这两种类型都是 Groovy 中的对象。
class IntTest {
Integer intWrapper
int intPrimitive
}

def test = new IntTest()
println "Integer: ${test.intWrapper}"
println "int: ${test.intPrimitive}"
输出:
Integer: null
int: 0
所以这也可以:
class StockItem extends DisplayableDomain {
int quantityOnHand
}
当我不希望属性为空时,我使用原始标识符(也是 Groovy 中的对象)来避免此类错误。

关于Grails 列默认值未默认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29151400/

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