JPA @Size annotation for BigDecimal

若如初见. 提交于 2019-12-20 18:29:11

问题


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;

回答1:


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;



回答2:


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;



回答3:


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



来源:https://stackoverflow.com/questions/20907923/jpa-size-annotation-for-bigdecimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!