Java 使用集合时的约束冲突属性路径
我无法集中精力解决JSR303 bean验证需求的问题(我目前正在使用Hibernate Validator) 假设我有以下域模型Java 使用集合时的约束冲突属性路径,java,hibernate-validator,Java,Hibernate Validator,我无法集中精力解决JSR303 bean验证需求的问题(我目前正在使用Hibernate Validator) 假设我有以下域模型 class Foo { private Set<Bar> bars = new HashSet<>(); @Valid public Set<Bar> getBars() { ... } } class Bar { private String name; @NotBlank public
class Foo {
private Set<Bar> bars = new HashSet<>();
@Valid
public Set<Bar> getBars() { ... }
}
class Bar {
private String name;
@NotBlank
public String getName() { ... }
}
class-Foo{
private Set bar=new HashSet();
@有效的
公共集getBars(){…}
}
分类栏{
私有字符串名称;
@不空白
公共字符串getName(){…}
}
假设我有一个foo
实例,其中有两个bar
s,其中一个名称为空。验证foo
后,我手里拿着一个@NotBlank
约束冲突,属性路径条[]。name
。这很好,但是
有没有办法让我找出这两个酒吧中哪一个的名字是空的?或者我被迫在这里使用
列表
,然后使用反射来内省-然后是唯一的-属性路径?Hibernate Validator的最新版本为此提供了一个解决方案。可以展开特性路径节点并获取特性值。这样您就可以知道哪个条实例受到影响:
Set<ConstraintViolation<Foo>> constraintViolations = ...;
Path path = constraintViolations.iterator().next().getPropertyPath();
Iterator<Path.Node> nodeIterator = path.iterator();
Path.Node node = nodeIterator.next();
Bar bar = (Bar) node.as( PropertyNode.class ).getValue();
Set constraintViolations=。。。;
路径路径=constraintViolations.iterator().next().getPropertyPath();
迭代器nodeIterator=path.Iterator();
Path.Node Node=nodeIterator.next();
Bar=(Bar)node.as(PropertyNode.class).getValue();
唉,这正是我需要的!不幸的是,我还不能更新到Hibernate Validator 5.2.0.CR1,因为它还没有发布(公司政策)。我想我现在必须坚持使用List
s+1为我们指明了光明的未来。