JSR303 custom validators being called twice

半腔热情 提交于 2019-11-27 22:26:12

Maybe the second validation is done by hibernate when you are sending your bean to the datastore. To turn it off add this to your persistence.xml:

<property name="javax.persistence.validation.mode" value="none"/>

https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/configuration.html says:

By default, Bean Validation (and Hibernate Validator) is activated. When an entity is created, updated (and optionally deleted), it is validated before being sent to the database. The database schema generated by Hibernate also reflects the constraints declared on the entity.

You can fine-tune that if needed:

AUTO: if Bean Validation is present in the classpath, CALLBACK and DDL are activated.

CALLBACK: entities are validated on creation, update and deletion. If no Bean Validation provider is present, an exception is raised at initialization time.

DDL: (not standard, see below) database schemas are entities are validated on creation, update and deletion. If no Bean Validation provider is present, an exception is raised at initialization time.

NONE: Bean Validation is not used at all

The first one is obviously done by your Spring controller because of @Valid annotation.

You can just add this property in your application.property files to disable hibernate validation spring.jpa.properties.javax.persistence.validation.mode=none

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