0

If I use the annotation @Size(min = 1, max = 50) in Hibernate, I can write a string to the database with at least one and at most 50 characters.

But now I need to restrict the possible values to only two numbers: 12342 and 13409.

Is there any annotation that allows this?

1 Answer 1

4

You can use the Pattern annotation for example:

@Pattern(regexp="12342|13409")
private String string;

Otherwise you can write a custom constraint.

See the documentation for further information.

https://docs.jboss.org/hibernate/validator/5.2/reference/en-US/html/chapter-bean-constraints.html#section-builtin-constraints

https://docs.jboss.org/hibernate/validator/5.2/reference/en-US/html/validator-customconstraints.html

Sign up to request clarification or add additional context in comments.

1 Comment

pls accept the answer, if it's what you are looking for ;)

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.