Tuesday, February 21, 2012

Grails nullable boolean and integer fields

When your GORM domain object fields are defined as Java primitives like boolean it can only be specific value like true or false. But sometimes you may want to have also third value for undefined values or NULL as in database. With primitives you will get error like:
Executing action [edit] of controller [com.test.TestController] caused exception: Null value was assigned to a property of primitive type setter of com.test.TestObject.testproperty; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.test.TestObject.testproperty

To enable nulls for such fields, you just have to change type from boolean primitive to Boolean object and that is it.

No comments:

Post a Comment