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.
Author by
Rafael Roque
Updated on July 09, 2022Comments
-
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 typeNUMBER(1,0)
.How can I use the Restrictions API to achieve the following functionality?
criteria.add(Restrictions.eq("active"),object.isActive());
-
Kaypro II almost 7 yearsA null Boolean can mean unknown or unspecified. Not to say you always need that distinction.
-
idmitriev almost 7 yearsin 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 about 6 yearsSorry, but "boolean" means true or false, nothing more. "Boolean" means true, false, or unspecified. Just like the difference between "long" and "Long"
-
idmitriev about 6 yearshow 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 about 6 yearsIf 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 about 2 yearsjust for those who are wondering, number(1) is mapped to true and number(0) to false