Hibernate注释字符串转换为Decimal

时间:2013-09-09 19:27:19

标签: java spring hibernate

我使用Spring + Hibernate来更新前端webapp的产品价格。 在后端,我有以下代码:

@NotNull(message = "Price is required")
@Range(min = 0, message = "Price must be positive.")
private BigDecimal price;

当我测试我的webapp时,我输入了没有数字的价格文本框,只有像"."这样的小数点。服务器给我一个错误信息:

    "Failed to convert property value of type 'java.lang.String' to required type 'java.math.BigDecimal' 
for property 'Price'; nested exception is java.lang.NumberFormatException"

我该如何解决这个问题?谢谢。

1 个答案:

答案 0 :(得分:0)

在尝试将输入字符串输入db之前,需要验证输入字符串。 "."不是有效号码。 "0.""0.0"将是有效的数字。