HibernateValidator 5似乎不对JPA实体进行级联验证 [英] HibernateValidator 5 seems to not cascade validation on JPA entities

查看:61
本文介绍了HibernateValidator 5似乎不对JPA实体进行级联验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Hibernate和Hibernate Validator 5时遇到问题.我有一些实体,例如 Group 和另一个实体 Person .它们之间的关系如下:组对人"有两个引用-联系人和经理.它们都是一对一的关系,具有完整的级联和孤立的删除选项.

I have a problem with Hibernate and Hibernate Validator 5. I have some entity, let's say Group and another entity Person. They are related as follows: Group has two references to Person - contact person and manager. They are both one-to-one relationships with full cascade and orphan removal options.

我想要的是在保存组时验证联系人和经理.此外,我希望使用其他验证组来验证联系人和经理.为此,我将 @ConvertGroup(from = Default.class,到= ContactPersonValidation.class) @Valid 一起放在联系人字段之前,然后执行了此操作类似于经理字段(使用不同的验证组).

What I want is to validate the contact person and the manager while the group is being saved. The more, I want a different validation group to be used to validate contact person and manager. In order to do this, I placed @ConvertGroup(from = Default.class, to = ContactPersonValidation.class) together with @Valid before the contact person field and I did it analogously for manager field (using different validation group).

现在,Hibernate验证不起作用-我的意思是该组未转换为 @ConvertGroup 中提供的组.我遵循了Hibernate验证程序的源代码,似乎分别验证了Group对象和两个Person对象.因此,人员验证不会从组"对象中级联,并且不会转换验证组.

Now, Hibernate validation does not work - I mean that the group is not converted to the one provided in @ConvertGroup. I followed the source code of Hibernate validator and it seems to validate Group object and two Person objects separately. Therefore, Person validation is not cascaded from Group object and the validation group is not converted.

您是否遇到过类似的问题并且知道如何解决?

Have you ever experienced a similar problem and know how to solve it?

推荐答案

在JPA触发的生命周期验证期间,Bean验证使用 TraversableResolver ,如果标记有 @Valid (请参阅JPA 2.0规范的3.6.1.2生命周期事件后自动验证的要求".)

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).

因此,您的 Person 对象将不会通过跟踪 Group 中的引用来进行验证,但是当它们本身持久存在时,它们将得到验证.因此,在 Group 上声明的组转换不适用.

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.

您可以通过定义 GroupSequenceProvider 用于 Person (请注意,这是特定于Hibernate Validator的功能).为此,您必须将一个人的角色"(例如,具有值 Contact Manager 的枚举形式)传递给 Person 实例.然后,如果持久存在给定的 Person 实例,则默认的组序列提供程序可以访问该角色并应用一个或另一个验证组.

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.

这篇关于HibernateValidator 5似乎不对JPA实体进行级联验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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