23

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;
4
  • 1
    yes I know the differences between boolean and Boolean, my application is throwing this exception because spring/hibernate is trying to set atShop to null instead of false, but how can i get it to set it to false and not null? Oct 22, 2012 at 14:15
  • 3
    Gee, I hate it when people downvote answers without any explanation and only because they are not exactly what they are looking for. Above all in a question like this which was not originally clear enough. It makes me lose my will to try to help. Oct 22, 2012 at 14:20
  • @Edwin Dalorzo: But it was not me who down voted the answers, that is why i added the edit section to my question because it was my fault that i did not explain that i already understood the differences between boolean and Boolean Oct 22, 2012 at 14:22
  • As soon as I know, boolean in the database will be mapped as 1 or 0, so you don't need columnDefinition. If you want to be sure, you can add the mapping in the persistence file like that <property name="hibernate.query.substitutions">true 1, false 0</property> Oct 22, 2012 at 14:37

6 Answers 6

48

- 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.

1
  • AutoBoxing (i.e. boolean --> Boolean) always works just fine. But what you really should be careful with is AutoUnboxing (i.e. Boolean --> boolean), because it throws NullPointerException if your Boolean wrapper object is null. Usually, using something like BooleanUtils.isTrue() or Boolean.TRUE.equals() is a good idea, when you need to unbox a wrapper Boolean object in an NPE-safe manner.
    – Yoory N.
    Jan 21, 2019 at 14:25
10

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)

4
  • i tried updating the column in the table to have a default value of 0 (ALTER TABLE Shop ALTER COLUMN atShop bit NOT NULL DEFAULT (0)) but i still get the same error Oct 22, 2012 at 14:54
  • You probably need to fix all the values that you've currently saved, they will be null. You should set the column to be NOT NULL too. Oct 22, 2012 at 14:57
  • i ended up completely dropping the column from the table and adding it back in using ALTER TABLE Shop ADD atShopbit NOT NULL DEFAULT (0) and even though all the values are set to 0 and everything looks ok the error is still appearing Oct 22, 2012 at 14:59
  • @HipHipArray For the existing column try ALTER TABLE Shop ADD DEFAULT 0 FOR atShop. Apr 14, 2017 at 10:45
4

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.

1

boolean is primitive it could be true or false and default is false. To obtain null, it's object type is Boolean.

0

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.

-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.

1
  • 2
    That works if you're in control of the code where the assignment is done but in this case the OP is getting values from Hibernate, which does all the assignments and mappings for you, so without implementing his own Type handler for Hibernate, as mentioned in Steve Ebersole's answer above, this is not really an option Dec 7, 2015 at 20:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.