2

I want to validate the size of an integer using Hibernate. I have been testing different annotations such as @Size, @Length and @Range but I don't seem to be able validate the specific size of an Integer. They seem to work for Strings, but for Integers its fine for a range but not a specific number. I want to ensure my SortCode field is only 6 digits long.

@Column(name = "SortCode", nullable = false)
// @Size(max = 6, min = 6)
// @Length(min = 0, max = 6)
@NotNull(message = ErrorConstants.SORTCODENULL)
//@Range(min = 000000)
private Integer SortCode;

1 Answer 1

2

Yes you can - either you create your own constraint and validator (see https://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html/validator-customconstraints.html) or you use either

@Min(100000)
@Max(999999)
private Integer SortCode;

or

@DecimalMin("100000")
@DecimalMax("999999")
private Integer SortCode;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, previously I used @Min(000001) @Max(999999) but it wasn't working for the min check. With your example it does work now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.