0
Class MyModel {

    public interface ServerGroup {}
    @NotNull
    private String name;

    @Size(min = 1, groups=ServerGroup.class)
    //@NotEmpty(groups=ServerGroup.class) // Tried this as well but not working!
    @OneToMany
    //@OneToMany(fetch=FetchType.EAGER) It works! Don't really want to do eager fetch
    private List<Person> persons = new ArrayList<Person>();


  //getters and setters here
  .....
}


Class Mytest {

@Test
public void testMyModel
MyModel myModel = new MyModel();
myModel.setName("This is testing");
persist(myModel); //persisting to database

//Now loading back again from database
MyModel myModel = MyModel.Service.findMyModelByName("This is testing");
Set<ConstraintViolation<MyModel>> violations = validator.validate(myModel,ServerGroup.class);
Assert.assertEquals(1, violations.size()); //It fails here saying no violation
}

如您所见,从数据库中创建并加载 mymodel,其中人员为空,因此尝试针对 ServerGroup 'group' 运行验证但没有违规,如果我将其更改为急切加载,那么它可以工作吗?请有任何建议。

4

0 回答 0