Mapping Java boolean to Oracle Number column with JPA and Hibernate

一曲冷凌霜 提交于 2020-12-04 16:01:35

问题


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

回答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.




回答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.



来源:https://stackoverflow.com/questions/28865222/mapping-java-boolean-to-oracle-number-column-with-jpa-and-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!