Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping Java boolean to Oracle Number column with JPA and Hibernate

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());
like image 236
Rafael Roque Avatar asked Mar 04 '15 21:03

Rafael Roque


2 Answers

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.

like image 82
Vlad Mihalcea Avatar answered Sep 18 '22 18:09

Vlad Mihalcea


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.

like image 42
idmitriev Avatar answered Sep 21 '22 18:09

idmitriev