Implementing cross-validation in java

假如想象 提交于 2019-12-24 20:35:22

问题


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:


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.



来源:https://stackoverflow.com/questions/11451746/implementing-cross-validation-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!