Wednesday, May 06, 2009

0.0 Is A BigDecimal In Groovy!

When you are so used to the convenience offered by Groovy and Grails, sometimes you are caught by simplicity itself.

I created a domain class recently:

class Foo {
...
double price

static constraints = {
price(min: 0.0)
}
}


Boom! The application failed to run with the error message:

Parameter for constraint [min] of property [value] of class [class Foo]
must be the same type as property: [double]


The cause of the problem is that I declared price to be a double, but 0.0 is a BigDecimal in Groovy. To fix the problem, change the value to 0.0d. I found the answer from this thread.

No comments: