Paul Waldo
Paul Waldo

Reputation: 1233

Grails column default value not defaulting

I'm using Grails 2.4.3 and have this Domain class:

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"
}
}

When I try to add a StockItem, I get this error:

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]

Apparently the default value for quantityOnHand is not getting set. I've tried placing the default value in quotes as well as a stand-alone integer value. I have also tried setting quantityOnHand to nullable. This prevents the error, but the column is null.

Upvotes: 4

Views: 1387

Answers (1)

Maicon Mauricio
Maicon Mauricio

Reputation: 3001

As @Biswas commented, a simple assignment at attribute definition solves the problem of default values.

class StockItem extends DisplayableDomain {
    Integer quantityOnHand = 0
}

Also, in OP's case, using int would solve the problem. Wrapper classes like Integer, Double and Boolean have their values set to null by default, although both types are objects in Groovy.

class IntTest {
    Integer intWrapper
    int intPrimitive
}

def test = new IntTest()
println "Integer: ${test.intWrapper}"
println "int: ${test.intPrimitive}"

Output:

Integer: null
int: 0

So this would also work:

class StockItem extends DisplayableDomain {
    int quantityOnHand
}

When I don't want the attribute to be nullable I use the primitive identifiers (also objects in Groovy) to avoid these sort of errors.

Upvotes: 2

Related Questions