How to insert a null value in a Boolean attribute when using Hibernate?

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

+5
source share
3 answers

This is a strange problem. An attribute Booleanwith a value nullshould definitely be stored as null, and the column type tinyintallows this. I cannot investigate the problem myself, but here is what I will do:

First, I would activate the registration of Hibernate DML statements and JBDC parameters to confirm that Hibernate is actually transmitting null(and it should). The relevant categories (chapter 3.5. Record ) are org.hibernate.SQLand org.hibernate.type.

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

Which JDBC driver (and version) are you using, by the way? If you are using the Microsoft driver, try using the latest version of jTDS (and vice versa).

+3
source

, Boolean, Boolean.

, Hibernate JPA, @Column(nullable=true), Hibernate, NULL .

+4

, , , , . -, , char (1), "Y", "N" "null". , , hibernate.cfg.xml: -

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

u hibernate.properties, : -

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

boolean. type = "boolean" .

+2

All Articles