I get the following error when using a primitive attribute in my grails domain object:
Null value was assigned to a property of primitive type setter of MyDomain.myAttribute
org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of MyDomain.myAttribute
at grails.orm.HibernateCriteriaBuilder.invokeMethod(HibernateCriteriaBuilder.java:1077)
use Integer as the type and provide setter/getter accordingly..
I'll try to make you understand with the help of an example. Suppose you had a relational table (STUDENT) with two columns and ID(int) and NAME(String). Now as ORM you would've made an entity class somewhat like as follows:-
Lets assume table already had entries. Now if somebody asks you add another column of "AGE" (int)
You'll have to set default values as NULL to add another column in a pre-filled table. This makes you add another field in the class. Now the question arises whether you'll be using a primitive data type or non primitive wrapper data type for declaring the field.
or
you'll have to declare the field as non primitive wrapper data type because the container will try to map the table with the entity. Hence it wouldn't able to map NULL values (default) if you won't declare field as wrapper & would eventually throw "Null value was assigned to a property of primitive type setter" Exception.
@Dinh Nhat, your setter method looks wrong because you put a primitive type there again and it should be:
A primitive type cannot be null. So the solution is replace primitive type with primitive wrapper class in your tableName.java file. Such as:
reference http://en.wikipedia.org/wiki/Primitive_wrapper_class to find wrapper class of a primivite type.