Mapping Java boolean to Oracle Number column with JPA and Hibernate

26,242

Solution 1

Hibernate maps the Boolean Java type to Oracle NUMBER(1,0) automatically.

So, you can use a Boolean value in your entity mappings, JPQL or Criteria queries and the generated SQL will use the database NUMBER(1,0) format instead.

Solution 2

I don't recommend to use Boolean, you should use boolean instead to prevent NPE, cause boolean value just has two available values - true or false. What does it mean null for boolean?? It's a rare case when you need wrapper type Boolean. Oracle - number(1) default 0 not null.

Share:
26,242
Rafael Roque
Author by

Rafael Roque

Updated on July 09, 2022

Comments

  • Rafael Roque
    Rafael Roque almost 2 years

    I have a property created like this in my model:

        public class Client {
            private Boolean active;
    }
    

    My RDBMS is Oracle and the active column is of type NUMBER(1,0).

    How can I use the Restrictions API to achieve the following functionality?

    criteria.add(Restrictions.eq("active"),object.isActive());
    
  • Kaypro II
    Kaypro II almost 7 years
    A null Boolean can mean unknown or unspecified. Not to say you always need that distinction.
  • idmitriev
    idmitriev almost 7 years
    in this case you should use Enum, which is suitable for this. Boolean means true or false, nothing more. If you need to have third state - use enum.
  • Gary Kephart
    Gary Kephart about 6 years
    Sorry, but "boolean" means true or false, nothing more. "Boolean" means true, false, or unspecified. Just like the difference between "long" and "Long"
  • idmitriev
    idmitriev about 6 years
    how unspecified should be treated for boolean type? If there are more than 3 values of some kind of something it's definitely a sign that it should be an enum.
  • riskop
    riskop about 6 years
    If the column is nullable, then use Boolean. Boolean can hold the third possibility, "unspecified". If the column is not nullable, then use boolean. Nullable column is not a reason to use Enum.
  • Tobi Tiggers
    Tobi Tiggers about 2 years
    just for those who are wondering, number(1) is mapped to true and number(0) to false