This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Boolean to int save using hibernate.

Jun 18th, 2007, 10:48 AM

Hi,

I am trying to save java.lang.Boolean to int datatype in sybase which is nullable using hibernate. if the Boolean object is null hibernate converts to 0 and save is it. Is there any way to make Boolean object null save as null to db?

A Boolean is either true or false, that's the very definition of a boolean.

A java.lang.Boolean object is just a wrapper around the boolean primitive type : if you don't instantiate this object, it will be considered as "false", hence it will be saved as "0" (which means "false").

This is a perfectly normal behavior, and hasn't got anything to do with Spring.

Comment

I will agree if it hibernate handle this way for primitive data type boolean, But for object Boolean it should have handled the same as it does for Integer. int in java doesn't allow null the same of primitive boolean and it handles Integer to proper conversion if its null it save null to database and I think the same should be handled for java.lang.Boolean.

Comment

A Boolean is either true or false, that's the very definition of a boolean.

A java.lang.Boolean object is just a wrapper around the boolean primitive type : if you don't instantiate this object, it will be considered as "false", hence it will be saved as "0" (which means "false").

This is a perfectly normal behavior, and hasn't got anything to do with Spring.

Boolean objects can be null. If you try to access them as a primitive value, they will throw a NullPointerException.

eg:

Code:

Boolean b = null;
if (b);

So in theory, Boolean have 3 states (null, false, true) while boolean only have 2.