我正在使用Grails 2.4.3并拥有此Domain类:
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的默认值未设置。我已经尝试将默认值放在引号中以及独立的整数值。我也尝试将quantityOnHand设置为可空。这可以防止错误,但列为空。