如何验证hibernate延迟加载集合大小?

时间:2013-01-21 21:58:34

标签: spring hibernate

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'运行验证但没有违规,如果我将其更改为急切加载,那么它是否有效?请提出任何建议。

0 个答案:

没有答案