Mike
Mike

Reputation: 205

Hibernate annotation String convert to Decimal

I am using Spring + Hibernate to update price of a product from front-end webapp. In the backend, I have the following codes:

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

When I test my webapp, I entered the price text box without numbers, just decimal dot like ".". Server gives me an error message:

    "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"

How should I solve this problem? Thanks ahead.

Upvotes: 1

Views: 745

Answers (1)

StormeHawke
StormeHawke

Reputation: 6207

You need to validate your input string before attempting to enter it into the db. "." is not a valid number. "0." or "0.0" would be valid numbers.

Upvotes: 1

Related Questions