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());

2 Answers 2

52

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.

Sign up to request clarification or add additional context in comments.

1 Comment

just for those who are wondering, number(1) is mapped to true and number(0) to false
1

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.

5 Comments

A null Boolean can mean unknown or unspecified. Not to say you always need that distinction.
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.
Sorry, but "boolean" means true or false, nothing more. "Boolean" means true, false, or unspecified. Just like the difference between "long" and "Long"
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.