Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify Double's precision on hibernate?

I try to create a table from hibernate annotations. I need to have a column of Double type, with the length specified like : (10,2). So SQL syntax show like:

... DOUBLE(10,2) .... 

I have tried to do that:

@Column(length = 10, precision = 2) ... 

but when i look at my created table, it is not specified the length for Double column. Does hibernate has solution for that or is it necessary to alter table configuration by hand?

Thanks!

like image 926
artaxerxe Avatar asked Nov 02 '10 14:11

artaxerxe


People also ask

What is precision in hibernate?

int - precision - (Optional) The precision for a decimal (exact numeric) column. (Applies only if a decimal column is used.) int - scale - (Optional) The scale for a decimal (exact numeric) column.

What is scale and precision in decimal?

Precision is the number of digits in a number. Scale is the number of digits to the right of the decimal point in a number. For example, the number 123.45 has a precision of 5 and a scale of 2. In SQL Server, the default maximum precision of numeric and decimal data types is 38.

What is @column in hibernate?

The @Column annotation is defined as a part of the Java Persistence API specification. It's used mainly in the DDL schema metadata generation. This means that if we let Hibernate generate the database schema automatically, it applies the not null constraint to the particular database column.


2 Answers

The length element of the Column annotation applies only if a string-valued column is used. In your case, you should use the precision and the scale elements.

@Column(precision=10, scale=2) 

Here is what the specification writes about them:

  • int - precision - (Optional) The precision for a decimal (exact numeric) column. (Applies only if a decimal column is used.)
  • int - scale - (Optional) The scale for a decimal (exact numeric) column. (Applies only if a decimal column is used.)

References

  • JPA 1.0 Specification
    • Section 9.1.5 "Column Annotation"
like image 192
Pascal Thivent Avatar answered Oct 13 '22 00:10

Pascal Thivent


@Column(name="Price", columnDefinition="Decimal(10,2) default '100.00'") 

Setting default values for columns in JPA

like image 30
Kris Avatar answered Oct 13 '22 01:10

Kris