使用 JPA 和 Hibernate 将 Java 布尔值映射到 Oracle Number 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28865222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:45:54  来源:igfitidea点击:

Mapping Java boolean to Oracle Number column with JPA and Hibernate

javadatabaseoraclehibernatehibernate-criteria

提问by Rafael Roque

I have a property created like this in my model:

我在我的模型中有一个这样创建的属性:

    public class Client {
        private Boolean active;
}

My RDBMS is Oracle and the activecolumn is of type NUMBER(1,0).

我的 RDBMS 是 Oracle,active列的类型是NUMBER(1,0)

How can I use the Restrictions API to achieve the following functionality?

如何使用 Restrictions API 来实现以下功能?

criteria.add(Restrictions.eq("active"),object.isActive());

回答by Vlad Mihalcea

Hibernate maps the BooleanJava type to Oracle NUMBER(1,0)automatically.

Hibernate自动将BooleanJava 类型映射到 Oracle NUMBER(1,0)

So, you can use a Booleanvalue in your entity mappings, JPQL or Criteria queries and the generated SQL will use the database NUMBER(1,0)format instead.

因此,您可以Boolean在实体映射、JPQL 或 Criteria 查询中使用值,并且生成的 SQL 将使用数据库NUMBER(1,0)格式。

回答by idmitriev

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.

我不建议使用布尔值,您应该使用布尔值来防止 NPE,因为布尔值只有两个可用值 - true 或 false。布尔值 null 是什么意思?当您需要包装类型布尔值时,这是一种罕见的情况。Oracle - number(1) 默认 0 不为空。