Cannot set boolean to null
hibernate, java, spring
Solution
- `boolean` is a primitive type, and can have a value of only `true or false`.
- Whereas `Boolean` is a Wrapper Object and can be given a null value.
- From `Java 1.5` `AutoBoxing` is provided, so you can convert boolean to Boolean and back to boolean with Simple assignment operator (`=`), So you can do this in places where you want Boolean instead of boolean.
Problem
I have a class Shop with the following variable ``` @Column(columnDefinition = "bit") private boolean atShop; ``` Using this value, I am using HSQL to retrieve this information from the application ``` from Person person left join fetch person.shop ``` when I try call this HSQL statement i get the following error ``` org.springframework.orm.hibernate3.HibernateSystemException: could not set a field value by reflection setter of com.test.dataobject.Shop.atShop; nested exception is org.hibernate.PropertyAccessException: could not set a field value by reflection setter of com.test.dataobject.Shop.atShop ``` It is throwing this because it is trying to set the boolean to null in the HSQL. I can solve this problem by changing `private boolean atShop;` to `private Boolean atShop;` but i want to keep this as a `boolean` as i am saving it as a bit in my database Is there a way to solve this without changing `boolean` to `Boolean`? EDIT: I know that boolean can only be true/false and Boolean can be set to null, but is there a way to get hibernate/spring to set this value to false(which i thought it should do automatically) instead of trying to set it to null and throwing this exception? I have also tried adding annotation to automatically set the value to false but this does not work either ``` @Column(nullable = false, columnDefinition = "bit default 0") private boolean atShop; ```