无法将布尔值设置为空 [英] Cannot set boolean to null

查看:951
本文介绍了无法将布尔值设置为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下变量的商店:

  @Column(columnDefinition =bit)
private boolean atShop;

使用这个值,我使用HSQL从应用程序中检索这些信息

  from Person person 
left join fetch person.shop

当我尝试调用这个HSQL语句时,我得到以下错误:

$ p $ org.springframework。 orm.hibernate3.HibernateSystemException:无法通过com.test.dataobject.Shop.atShop的反射设置器设置字段值;嵌套异常是org.hibernate.PropertyAccessException:无法通过com.test.dataobject.Shop.atShop的反射设置器设置字段值

它抛出这个是因为它试图在HSQL中将布尔值设置为null。我可以通过更改私人布尔atShop; 私人布尔atShop; 来解决这个问题,但我想保留这个作为 boolean 因为我将它保存在我的数据库中



有没有办法解决这个问题, code>布尔到布尔



编辑:



我知道布尔值只能是true / false,布尔值可以设置为null,但是有没有办法让hibernate / spring设置这个值到假(我认为它应该自动执行),而不是试图将其设置为空,并引发此异常?



我也尝试添加注释来自动设置值为false,但这也不起作用

$ $ p $ $ $ c $ @Column(nullable = false,columnDefinition =bit default 0)
私人布尔atShop;


解决方案

- boolean 是一个原始类型,并且可以只有 true或false



- 鉴于布尔值包装对象一个 null 值。



- Java 1.5 提供了 AutoBoxing ,所以你可以将布尔转换为布尔值,然后返回布尔值,使用简单的赋值操作符( = ),所以你可以在你想要布尔而不是布尔值的地方做到这一点。


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;

解决方案

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

这篇关于无法将布尔值设置为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆