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;
有帮助吗?

解决方案

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;

其他提示

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;
@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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top