46

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!

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
artaxerxe
  • 6,281
  • 21
  • 68
  • 106
  • Sounds like you want the Decimal data type. I think floats/doubles only have precision parameter. – dotjoe Nov 02 '10 at 21:46

4 Answers4

98

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"
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 14
    Very good. It works with "BigDecimal" Java type. But with "Double" does not. I have changed only the type and now work:). – artaxerxe Nov 03 '10 at 06:20
  • 2
    @artaxerxe Any idea how can it work for "java.lang.Double"? :) – prateekmathur1991 Mar 24 '17 at 05:56
  • 1
    @plutonium1991 if you need the precision, you probably need to use `BigDecimal` instead.. that's why they probably do not allow this annotation on Doubles.. explanation here -> https://stackoverflow.com/a/3413493 – Felipe S. Aug 10 '17 at 01:54
  • @plutonium1991 yes, either add custom columnDefinition as was answered by Kris or see my answer for Hibernate. – zeratul021 Jul 25 '18 at 19:05
  • The JavaDoc for `@Column` `precision` and `scale` says "Applies only if a decimal column is used." that's why it doesn't work for `DOUBLE` columns. For that you need `columnDefinition="DOUBLE(10,2)"`. – Dario Seidl Sep 05 '22 at 11:10
30
@Column(name="Price", columnDefinition="Decimal(10,2) default '100.00'")

Setting default values for columns in JPA

Community
  • 1
  • 1
Kris
  • 963
  • 1
  • 13
  • 22
5

You can also use @Digits from the hibernate validator API which implements the javax.validation Bean Validation standard

@Digits(integer = 10 /*precision*/, fraction = 2 /*scale*/)

From the Javadocs

The annotated element must be a number within accepted range Supported types are:

  • BigDecimal
  • BigInteger
  • CharSequence
  • BigInteger
  • byte, short, int, long, and their respective wrapper types

null elements are considered valid

Community
  • 1
  • 1
FearlessHyena
  • 3,527
  • 33
  • 39
  • This can be useful, but does something different: this validates the value before the entity is persisted and throws an exception if the value is out of the specified range. It doesn't change the database column definition. For example, if your database is accessed by another application, it could still write values of out that range into the column. – Dario Seidl Sep 05 '22 at 11:08
5

Use @Type (only in Hibernate):

@Column(precision = 5, scale = 4)
@Type(type = "big_decimal")
private double similarity;

Will result in definition (PostgreSQL, Oracle):

similarity numeric(5, 4),
similarity number(5, 4)
zeratul021
  • 2,644
  • 3
  • 32
  • 45