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

时间:2009-11-10 18:37:17

标签: oracle hibernate orm

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

@NotNull
boolean myBoolean;

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

3 个答案:

答案 0 :(得分:55)

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

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

或将其映射到'Y'或'N'

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

答案 1 :(得分:20)

简单编号(1)

如果需要,可以使用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>);

答案 2 :(得分:6)

这是你真正需要的

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

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

如上所述 Hibernate docu