Question

I set the field as Number like below in the oracle database.

      name             type    length      scale
EMP_GENDER NUMBER   0             0

After the hibernate reverse engineering,this filed's type becomes to BigDecimal.

private BigDecimal empGender;

But when I set the length to 2 in database,it could be right and field type in the entity become to Integer.
How did this happen?

Était-ce utile?

La solution

When Hibernate reverse engineers your schema, that will use Oracle's Mete-data tables to extract table and columns information.
One of the them will be user_tab_columns View.

Having

create table EMP (EMP_GENDER number);

To extract EMP table,s column information you may use:

select *
from user_tab_columns
where user_tab_columns.TABLE_NAME = 'EMP'

The query result will be characterization of every column of EMP table.
We will see DATA_TYPE , DATA_LENGTH and DATA_PRECISION columns.
(using Oracle 10 g)
enter image description here

Having:

create table EMP (EMP_GENDER number(2));

We will see enter image description here

When not defining precision for numeric type, the DATA_LENGTH (default is 22 ) will be treated as precision.
So a number with length of 22 will be translated to Big-decimal by Hibernate(that will exceed the maximum precision of decimal data type)
When numeric type precision is equals to 2, the Integer data type will be sufficient.

You must specify the exact numeric precision on table creation to prevent the problem.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top