Mohammad Faisal
Mohammad Faisal

Reputation: 5959

restrict length of number column using jpa

I would like to generate a column (abc NUMBER(2)) (at the moment) in oracle:

@Column(length = 2)
private Integer abc;

but still in Oracle, the column created is abc NUMBER(10, 0).

I tried @Digits(integer=2, fraction=0) but I think this would not work as the Java type is Integer.

Java doc for @Digit says:

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

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

null elements are considered valid

but @Digits(integer=2, fraction=0) has no effect on the generated column.

I am looking for a generic solution which could also work on MySQL also.

Upvotes: 1

Views: 2915

Answers (1)

Davide Cavestro
Davide Cavestro

Reputation: 497

You could try the precision obtained by java-sql data type mapping

@Column
private Short weightedScore;

Give also a try to BigDecimal specifying precision and scale

@Column(precision = 2, scale = 0)
private BigDecimal weightedScore;

IMHO BigDecimal precision maps quite well with Oracle NUMBER(precision).

Use the precision attribute, i.e.

@Column(precision=2)
private Integer abc;

I'd expect it generates some DDL such as NUMBER(2,0).

See How to specify Double's precision on hibernate?

Upvotes: 3

Related Questions