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?

balteo
  • 23,602
  • 63
  • 219
  • 412
  • 1
    You can find good and full cross field validation example here http://stackoverflow.com/questions/1972933/cross-field-validation-with-hibernate-validator-jsr-303 – masted Jul 13 '12 at 08:07

1 Answers1

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.

Marius
  • 3,043
  • 1
  • 15
  • 24
  • 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? – balteo Jul 12 '12 at 13:07
  • 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. – Marius Jul 12 '12 at 14:47
  • Thanks Marius for your detailed reply. – balteo Jul 12 '12 at 15:27