且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果@Constraint(通过= {}验证),则使用Hibernate验证程序来验证约束

更新时间:2023-09-24 12:58:04

javax.validation.constraints 将验证器留给实现。 Hibernate为这些约束提供验证器,并在引导时注册它们。

The javax.validation.constraints leaves the validators to the implementation. Hibernate provides validators to these constraints, and registers them when bootstrapping.

您可以通过XML设置验证器,而无需使用 @Constraint(validatedBy = {})。但是,这通常用于添加/替换您自己提供的验证器。我不确定它会如何帮助您,因为您仍然需要引用约束验证器。

You can set the validators via XML without using @Constraint(validatedBy = { }). However, this is usually used to add to/replace the provided validators with your own. I'm not sure how it will help you since you still need to reference the constraint validator.

使用XML约束定义的示例:

Example of using XML constraint definition:

<constraint-definition annotation="org.mycompany.CheckCase">
    <validated-by include-existing-validators="false">
        <value>org.mycompany.CheckCaseValidator</value>
    </validated-by>
</constraint-definition>

请参阅:通过XML配置

即将推出的5.2版本提供其他方式,例如使用服务加载程序或实现 ConstraintDefinitionContributor 。请参阅:提供约束定义

The upcoming 5.2 release provides more ways such as using a service loader or implementing ConstraintDefinitionContributor. See: Providing constraint definitions