0

I use Spring Roo + jpa + hibernate and I would like to implement cross-validation (validation of several fields at the same time) in my application.

I am not sure how to go about implementing it. Can anyone please advise me and/or direct me to relevant documentation?

1

1 Answer 1

1

Have a look at Hibernate Validator, which allows entity validation (using annotations).

http://www.hibernate.org/subprojects/validator.html

In short, you annotate your field constraints by placing hibernate validator/ JPA annotations above them. (E.g. @Min(10)) and use the following piece of code to find any invalid fields;

ValidatorFactory factory = Validation.byDefaultProvider().configure().traversableResolver(new CustomTraversableResolver() ).buildValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<BaseValidationObject>> constraintViolations = Validator.validate(myEntityToValidate);

If you need to validate specific relationships between entities, you can write custom validators to fit that need.

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

3 Comments

Thanks! I have had a look at the Hibernate Validator documentation and I am starting to get the hang of it. There is one thing I don't really understand though: where is located the bootstrapping you have included in a spring mvc application?
Hi Balteo, i believe thats not needed, just add the Hibernate validator jars and you should be good to go. Let me know if you run into difficulties and i'll supply some more in depth examples.
Thanks Marius for your detailed reply.

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.