HibernateValidator 5 seems to not cascade validation on JPA entities

别说谁变了你拦得住时间么 提交于 2019-12-09 03:37:34

During lifecycle validation triggered by JPA, Bean Validation uses a TraversableResolver which doesn't follow up assocations also if they are marked with @Valid (see 3.6.1.2 "Requirements for Automatic Validation upon Lifecycle Events" of the JPA 2.0 spec).

So your Person objects won't be validated by following up references from Group but they will be validated when they themselves are persisted. Thus the group conversions declared on Group don't apply.

You can implement the behavior you want by defining a GroupSequenceProvider for Person (note that this is a Hibernate Validator specific feature). For that purpose, you would have to pass the "role" of a person (e.g. in form of an enum with values Contact and Manager) to the Person instances. The default group sequence provider could then access the role and apply one or the other validation group if a given Person instance is persisted.

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