Empty validatedBy in @Constraint

十年热恋 提交于 2019-12-04 10:27:44

问题


I noticed that all built-in constraints have an empty value for the validatedBy parameter in @Constraint. i.e. @Constraint(validatedBy = {})

First, why are they allowed to have an empty value for validatedBy? I thought you can leave it blank only for constraint composition that does not need addtional validation?

Also, note that the Hibernate Validator can still find a validator implementation class for each built-in constraint, despite the validatedBy is empty, but if I leave the validatedBy blank for my constraint, my custom validator never gets picked up. Why is that?

Thanks.


回答1:


Those built-in are treated in special implementation-specific way and their validators are configured programmatically.

For Hibernate Validator it's done in ConstraintHelper.java. I think you can't achieve the same for your custom constraints.




回答2:


Maybe have a look at this answer:

How to avoid cross dependency between layers because of @Constraint validatedBy?

And those two links:

Adding constraints programmatically: http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html/validator-specifics.html#section-programmatic-api

Adding constraints per xml: http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-xmlconfiguration.html



来源:https://stackoverflow.com/questions/4512826/empty-validatedby-in-constraint

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