Announcement Announcement Module
Collapse
No announcement yet.
Boolean to int save using hibernate. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Boolean to int save using hibernate.

    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?

    Thanks,
    Hemen

  • #2
    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


    • #3
      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


      • #4
        A boolean is either true or false. That's the very idea behind booleans.
        If you want something more complex, then why don't you use an int directly?

        Comment


        • #5
          Originally posted by julien.dubois View Post
          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.

          Comment


          • #6
            You're perfectly right at the JVM level.

            But if you don't instanciate any object, what do you expect Hibernate to save?

            Comment


            • #7
              Originally posted by julien.dubois View Post
              But if you don't instanciate any object, what do you expect Hibernate to save?
              Can't speak for Hibernate :-) but from a specification point of view (such as JDO2) when you have a field that is a Boolean and it is not set in the users object at the point of persisting, and the column in the datastore is nullable then it should save null and preserve the aforementioned 3 states. When the object is retrieved the Boolean field is then still null.

              Comment

              Working...
              X