默认情况下,使用 Oracle 数据库时,Hibernate 将布尔数据类型映射到什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1710198/
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-10 02:13:38  来源:igfitidea点击:

What does Hibernate map a boolean datatype to when using an Oracle database by default?

oraclehibernateorm

提问by Peter D

By default if I create a field in an entity like:

默认情况下,如果我在实体中创建一个字段,例如:

@NotNull
boolean myBoolean;

And I let Hibernate auto-create my tables. What Oracle data type will this map to?

我让 Hibernate 自动创建我的表。这将映射到什么 Oracle 数据类型?

采纳答案by Arthur Ronald

Simply Number(1)

简单数(1)

If you want, use SchemaExport to generate a script to your target database. Something like

如果需要,请使用 SchemaExport 为目标数据库生成脚本。就像是

AnnotationConfiguration configuration = new AnnotationConfiguration();

configuration
    .addAnnotatedClass(<TYPE_YOUR_CLASS>.class)
    .setProperty(Environment.USER, <TYPE_YOUR_USER>)
    .setProperty(Environment.PASS, <TYPE_YOUR_PASSWORD>)
    .setProperty(Environment.URL, <TYPE_YOUR_URL>)
    .setProperty(Environment.DIALECT, <TYPE_YOUR_DIALECT>)
    .setProperty(Environment.DRIVER, <TYPE_YOUR_DRIVER>);

SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile("schema.sql");

schema.create(<DO_YOU_WANT_TO_PRINT_TO_THE_CONSOLE>, <DO_YOU_WANT_TO_EXPORT_THE_SCRIPT_TO_THE_DATABASE>);

回答by non sequitor

As @Arthur said it maps to Number(1)which would be the standard sql bit where 0 == falseand 1 == true. As an alternative you can map char(1)to 'T' or 'F' like this

正如@Arthur 所说,它映射到Number(1)哪个将是标准的 sql 位 where0 == false1 == true. 作为替代方案,您可以char(1)像这样映射到“T”或“F”

@org.hibernate.annotations.Type(type="true_false")
@NotNull
boolean myBoolean;

or map it to 'Y' or 'N'

或将其映射到“Y”或“N”

@org.hibernate.annotations.Type(type="yes_no")
@NotNull
boolean myBoolean;

回答by Gilberto Pe-Curto

This is what you really need

这才是你真正需要的

Java POJO:

Java POJO

//@Type(type="true_false") //not working for '1' and '0' in NUMERIC(1) field
@Type(type= "org.hibernate.type.NumericBooleanType")
@NotNull(message="NOT_NULL")
@Column(name = "IS_DELEGATION", nullable = false)
private Boolean isDelegation;

Oracle DDL

甲骨文 DDL

alter table agent add (is_delegation number(1) default 0 not null);

As stated in Hibernate docu

Hibernate 文档中所述