23

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());
Rafael Roque
  • 377
  • 2
  • 5
  • 14

2 Answers2

49

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.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
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.

idmitriev
  • 4,619
  • 4
  • 28
  • 44
  • 11
    A null Boolean can mean unknown or unspecified. Not to say you always need that distinction. – Kaypro II May 31 '17 at 22:07
  • 1
    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. – idmitriev Jun 01 '17 at 08:33
  • 3
    Sorry, but "boolean" means true or false, nothing more. "Boolean" means true, false, or unspecified. Just like the difference between "long" and "Long" – Gary Kephart Mar 26 '18 at 18:36
  • 1
    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. – idmitriev Apr 05 '18 at 08:05
  • 2
    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. – riskop Apr 27 '18 at 08:32