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!
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.
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.
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.
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.)
@Column(name="Price", columnDefinition="Decimal(10,2) default '100.00'")
Setting default values for columns in JPA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With