Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I asked this question on the <a href="https://forum.hibernate.org/viewtopic.php?f=9&amp;t=1010626&amp;start=0" rel="noreferrer">Hibernate forums</a> as well, and it got answered there. I wanted to share the answer here in case anyone else encounters this issue.</p> <p>Simply changing the Collection to a List solves the problem. Here's the updated unit test:</p> <pre><code>package com.foo; import java.util.ArrayList; import java.util.List; import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Valid; import javax.validation.Validation; import javax.validation.Validator; import org.hibernate.validator.constraints.NotEmpty; import org.junit.Test; import org.springframework.util.AutoPopulatingList; public class ValidatorTest { class Person { @NotEmpty private String name; @NotEmpty @Valid private List&lt;Foo&gt; list = new AutoPopulatingList&lt;Foo&gt;(Foo.class); public void setName(String name) { this.name = name; } public String getName() { return name; } public List&lt;Foo&gt; getList() { return list; } public void setList(List&lt;Foo&gt; foos) { this.list = foos; } } class Foo { @NotEmpty private String bar; public void setBar(String bar) { this.bar = bar; } public String getBar() { return bar; } } @Test public void testValidator() throws Exception { Foo foo0 = new Foo(); foo0.setBar(""); Foo foo1 = new Foo(); foo1.setBar(""); List&lt;Foo&gt; list = new ArrayList&lt;ValidatorTest.Foo&gt;(); list.add(foo0); list.add(foo1); Person person = new Person(); person.setName("Test Person"); person.setList(list); Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); Set&lt;ConstraintViolation&lt;Person&gt;&gt; violations = validator.validate(person); for (ConstraintViolation&lt;Person&gt; constraintViolation : violations) { System.out.println(constraintViolation); } } } </code></pre> <p>And the output from the above test (which now includes indexes):</p> <pre><code>ConstraintViolationImpl{interpolatedMessage='may not be empty', propertyPath=list[1].bar, rootBeanClass=class com.foo.ValidatorTest$Person, messageTemplate='{org.hibernate.validator.constraints.NotEmpty.message}'} ConstraintViolationImpl{interpolatedMessage='may not be empty', propertyPath=list[0].bar, rootBeanClass=class com.foo.ValidatorTest$Person, messageTemplate='{org.hibernate.validator.constraints.NotEmpty.message}'} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload