开发者

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!


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"


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

Setting default values for columns in JPA


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)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜