Reputation: 4753
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;
Upvotes: 25
Views: 77343
Reputation: 611
Thought I'd give my 2 cents on this since I had a similar issue but the solution was different.
So my issue was that it was retrieving data from the database and trying to save that data back. The data it retrieved for the boolean field was null. So when it tried to save it back it complained that it can't save null to a boolean.
The fix was to update the database data so none of these fields had null values.
Upvotes: 0
Reputation: 1
You do know you could just put a try catch around it if it throws an exception that means it was null, therefore set it to false.
Upvotes: -1
Reputation: 9443
null
is not the same as false
(nor is it the same as true
). null
has a very specific meaning. That is why Hibernate does what it does... because it is really the only thing that makes sense, despite what you think it should do.
If you want to instruct Hibernate to treat null
in those columns as false, the only real solution is to develop a custom Hibernate Type mapping for this special treatment of "null is not really a null". You can accomplish that by either implementing the org.hibernate.type.Type
interface, or the org.hibernate.usertype.UserType
interface. You best bet is to extend the standard boolean Type mapping and weave in the special null handling.
One thing to be careful of, however, is querying since checking equality against null
is never valid in ANSI SQL. It resolves to what SQL calls UNDEFINED
, which usually means FALSE
.
Upvotes: 4
Reputation: 798
boolean
is primitive it could be true
or false
and default is false
. To obtain null
, it's object type is Boolean
.
Upvotes: 1
Reputation: 4985
Rather than fiddle with the hibernate code, change your table definition to default null values to false (or 0). That way when you come to read from the database it will always have valid values (which, since it's boolean, makes more sense)
Upvotes: 12
Reputation: 33534
- 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.
Upvotes: 49