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

前端 未结 3 1669
南旧
南旧 2020-12-29 02:47

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 t

相关标签:
3条回答
  • 2020-12-29 03:23

    This is what you really need

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

    As stated in Hibernate docu

    0 讨论(0)
  • 2020-12-29 03:32

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

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

    or map it to 'Y' or 'N'

    @org.hibernate.annotations.Type(type="yes_no")
    @NotNull
    boolean myBoolean;
    
    0 讨论(0)
  • 2020-12-29 03:37

    Simply Number(1)

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

    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>);
    
    0 讨论(0)
提交回复
热议问题