How do I use the @Size
annotation for MySQL DECIMAL(x,y)
columns?
I'm using BigDecimal
, but when I try to include the @Size
max
it doesn't work. Here is my code:
@Size(max = 7,2)
@Column(name = "weight")
private BigDecimal weight;
The @Basic annotation has two attributes, optional and fetch. Let's take a closer look at each one. The optional attribute is a boolean parameter that defines whether the marked field or property allows null. It defaults to true.
BigDecimal precision is de facto unlimited since it is based on an int array of arbitrary length. Though operations with double are much faster than with BigDecimal this data type should never be used for precise values, such as currency.
A BigDecimal is an exact way of representing numbers. A Double has a certain precision. Working with doubles of various magnitudes (say d1=1000.0 and d2=0.001 ) could result in the 0.001 being dropped altogether when summing as the difference in magnitude is so large. With BigDecimal this would not happen.
You could use the Hibernate Validator directly, and annotate your field with @Digits like so:
@Digits(integer=5, fraction=2)
@Column(name = "weight")
private BigDecimal weight;
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