在Java中实现交叉验证 [英] Implementing cross-validation in java

查看:307
本文介绍了在Java中实现交叉验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring Roo + jpa + hibernate,并且我想在我的应用程序中实现交叉验证(同时验证多个字段).

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?

推荐答案

看看Hibernate Validator,它可以进行实体验证(使用批注).

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

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

简而言之,您可以通过在其上方放置休眠验证器/JPA注释来注释字段约束. (例如@Min(10)),并使用以下代码查找任何无效字段;

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.

这篇关于在Java中实现交叉验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆