Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 328
Peter D Avatar asked Nov 10 '09 18:11

Peter D


People also ask

What is Boolean data type in Oracle?

A BOOLEAN data type enables you to represent logical values. In code, BOOLEAN values are represented by values for "no" and "yes" (in any combination of uppercase and lowercase characters).

Do we have Boolean in Oracle?

The Oracle RDBMS does not support a Boolean datatype. You can create a table with a column of datatype CHAR(1) and store either “Y” or “N” in that column to indicate TRUE or FALSE. That is a poor substitute, however, for a datatype that stores actual Boolean values (or NULL).


2 Answers

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; 
like image 78
non sequitor Avatar answered Sep 24 '22 22:09

non sequitor


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>); 
like image 39
Arthur Ronald Avatar answered Sep 26 '22 22:09

Arthur Ronald