java 使用 Hibernate 时如何在布尔属性中插入空值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2951009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:38:14  来源:igfitidea点击:

How do I insert the null value in a Boolean attribute when using Hibernate?

javahibernatenullnullable

提问by Pedro Rolo

I have a class with a Boolean attribute. When I instantiate and persist this class, the Boolean attribute is stored with the "false" value instead of the expectable "null". How can I set a Boolean attribute to "Null"?

我有一个带有布尔属性的类。当我实例化并持久化此类时,布尔属性与“false”值一起存储,而不是可预期的“null”。如何将布尔属性设置为“空”?

采纳答案by Pascal Thivent

This is a weird problem. A Booleanattribute with a nullvalue is definitely supposed to be stored as NULLand the tinyintcolumn type allows that. I cannot investigate the problem myself but here is what I would do:

这是一个奇怪的问题。Boolean具有null值的属性肯定应该存储为NULL,并且tinyint列类型允许这样做。我自己无法调查这个问题,但我会这样做:

First, I would activate the logging of Hibernate's DML statements and of JBDC parameters to confirm that Hibernate is actually passing NULL(and it should). The relevant categories (chapter 3.5. Logging) are org.hibernate.SQLand org.hibernate.type.

首先,我将激活 Hibernate 的 DML 语句和 JBDC 参数的日志记录,以确认 Hibernate 确实通过了NULL(并且应该通过)。相关类别(第3.5日志记录)是org.hibernate.SQLorg.hibernate.type

If the first test doesn't reveal the problem, I would try to isolate it further with a simple test class using raw JDBC to insert a Booleanwith a nullvalue (and also read it). If this test is not positive, then I would start to suspect the JDBC driver.

如果第一个测试没有发现问题,我会尝试使用一个简单的测试类进一步隔离它,使用原始 JDBC 插入Boolean一个null值(并读取它)。如果这个测试不是肯定的,那么我会开始怀疑 JDBC 驱动程序。

What JDBC driver (and version) are you using by the way? If you are using the Microsoft driver, try with the latest version of jTDS(and inversely).

顺便说一下,您使用的是什么 JDBC 驱动程序(和版本)?如果您使用的是 Microsoft 驱动程序,请尝试使用最新版本的jTDS反之亦然)。

回答by Marc

First, make sure that you define a Booleanobject and not a booleantype.

首先,确保您定义的是Boolean对象而不是boolean类型。

Then, if you are using Hibernate with JPA attributes, then you can add @Column(nullable=true)to explicitly tell Hibernate the the column should have NULL as default.

然后,如果您将 Hibernate 与 JPA 属性一起使用,那么您可以添加@Column(nullable=true)以明确告诉 Hibernate 该列的默认值应为 NULL。

回答by M.J.

First you make sure that the object you are trying to persist is Boolean not primitive boolean. Secondly, you can define the column corresponding as char(1) which can hold 'Y', 'N' or null value. and lastly,and the most important add the following line to you hibernate.cfg.xml :-

首先,您要确保您尝试持久化的对象是布尔值而不是原始布尔值。其次,您可以将对应的列定义为 char(1) ,它可以保存 'Y'、'N' 或空值。最后,最重要的是向您添加以下行 hibernate.cfg.xml :-

<property name="query.substitutions">'Y'=true,'N'=false</property>

if u are using hibernate.properties file then use following:-

如果您使用的是 hibernate.properties 文件,则使用以下内容:-

hibernate.query.substitutions true 'Y', false 'N'

this would help you out to store null values to boolean type column. and don't forget to map the property as type="boolean" in the mapping file.

这将帮助您将空值存储到布尔类型列。并且不要忘记在映射文件中将属性映射为 type="boolean"。