18

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;
DataNucleus
  • 15,497
  • 3
  • 32
  • 37

3 Answers3

28

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;
mavroprovato
  • 8,023
  • 5
  • 37
  • 52
  • Would it work with TopLink's `javax.persistence_1.0.0.0_1-0-2.jar` as well? –  Jan 03 '14 at 16:30
  • 1
    Validation is a separate API from JPA. So, there will be no error when you try to persist the entity through `persist()` for example, you have to do the validation yourself – mavroprovato Jan 03 '14 at 16:35
  • How do I do the validation myself? Is there a link to Oracle's site where I could read more on that? –  Jan 03 '14 at 16:38
  • 1
    What I meant is that validation should be done before you try to save to the database, at the view layer. See http://docs.oracle.com/javaee/6/tutorial/doc/gircz.html if you are using JSF, or here if you are using Spring http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html – mavroprovato Jan 03 '14 at 16:45
  • 1
    Java EE 7: http://docs.oracle.com/javaee/7/tutorial/doc/bean-validation001.htm#GIRCZ Looks like `@DecimalMax` is the annotation to use? –  Jan 03 '14 at 16:51
19

See this answer

@Column(nullable= false, precision=7, scale=2)    // Creates the database field with this size.
@Digits(integer=9, fraction=2)                    // Validates data when used as a form
private BigDecimal myField;
Community
  • 1
  • 1
borjab
  • 11,149
  • 6
  • 71
  • 98
  • 1
    I think it should be @Column(nullable= false, precision=9, scale=2) and @Digits(integer=7, fraction=2) – Tony Wen Nov 10 '20 at 02:11
8
@Column(columnDefinition = "DECIMAL(7,2)")

If you're asking how you should validate, you should use the @Min and @Max annotations or the @DecimalMin and @DecimalMax annotations.

@Size is an annotation used to validate a property, not to define its column. @Size is typically used to assure that a string or a collection is of a certain size.

zmf
  • 9,095
  • 2
  • 26
  • 28
  • So how would I use `@Max` to validate a `BigDecimal` with `7,2`? –  Jan 03 '14 at 16:20
  • @Threat when you say 'validate a BigDecimal with 7,2' ... can you clarify that a bit? When I read that I assume you're speaking about a BigDecimal with a specific scale and precision, which is different from a min and max value. – zmf Jan 03 '14 at 16:32
  • In other words, my `BigDecimal` must conform to, for example, `9999999,99` maximum. The same way that MySQL would validate it as far as I understand. –  Jan 03 '14 at 16:35
  • @ThreaT, `DECIMAL(7,2)` means a total of 7 digits, 2 of which are after the decimal point. So the maximum value that it can hold is 99999,99 – mavroprovato Jan 03 '14 at 16:37
  • It is throwing me SQL Error: 0, SQLState: 22003, ERROR: numeric field overflow. source is BigDecimal field. I tried to put over @Column(columnDefinition = "DECIMAL(8,8)"), so there is something more I need to discover. – kensai Jan 30 '18 at 17:00