Mapping Java boolean to Oracle Number column with JPA and Hibernate

Rafael Roque picture Rafael Roque · Mar 4, 2015 · Viewed 22.5k times · Source

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

Answer

Vlad Mihalcea picture Vlad Mihalcea · Mar 4, 2015

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.