Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何验证hibernate延迟加载集合大小? classmymodel{ 公共接口服务器组{} @NotNull 私有字符串名称; @大小(最小值=1,组数=ServerGroup.class) //@NotEmpty(groups=ServerGroup.class)//也尝试了这个方法,但没有成功! @独身癖 //@OneToMany(fetch=FetchType.eanger)它可以工作!我真的不想做eanger fetch 私人列表人员=新的ArrayList(); //这里有接球手和接球手 ..... } 类Mytest{ @试验 公共void testMyModel MyModel MyModel=新的MyModel(); myModel.setName(“这是测试”); 持久化(myModel);//持久化到数据库 //现在再次从数据库中加载 MyModel MyModel=MyModel.Service.findMyModelByName(“这是测试”); 设置冲突=validator.validate(myModel,ServerGroup.class); Assert.assertEquals(1,违例项.size());//此处失败,表示没有违例项 }_Spring_Hibernate - Fatal编程技术网

Spring 如何验证hibernate延迟加载集合大小? classmymodel{ 公共接口服务器组{} @NotNull 私有字符串名称; @大小(最小值=1,组数=ServerGroup.class) //@NotEmpty(groups=ServerGroup.class)//也尝试了这个方法,但没有成功! @独身癖 //@OneToMany(fetch=FetchType.eanger)它可以工作!我真的不想做eanger fetch 私人列表人员=新的ArrayList(); //这里有接球手和接球手 ..... } 类Mytest{ @试验 公共void testMyModel MyModel MyModel=新的MyModel(); myModel.setName(“这是测试”); 持久化(myModel);//持久化到数据库 //现在再次从数据库中加载 MyModel MyModel=MyModel.Service.findMyModelByName(“这是测试”); 设置冲突=validator.validate(myModel,ServerGroup.class); Assert.assertEquals(1,违例项.size());//此处失败,表示没有违例项 }

Spring 如何验证hibernate延迟加载集合大小? classmymodel{ 公共接口服务器组{} @NotNull 私有字符串名称; @大小(最小值=1,组数=ServerGroup.class) //@NotEmpty(groups=ServerGroup.class)//也尝试了这个方法,但没有成功! @独身癖 //@OneToMany(fetch=FetchType.eanger)它可以工作!我真的不想做eanger fetch 私人列表人员=新的ArrayList(); //这里有接球手和接球手 ..... } 类Mytest{ @试验 公共void testMyModel MyModel MyModel=新的MyModel(); myModel.setName(“这是测试”); 持久化(myModel);//持久化到数据库 //现在再次从数据库中加载 MyModel MyModel=MyModel.Service.findMyModelByName(“这是测试”); 设置冲突=validator.validate(myModel,ServerGroup.class); Assert.assertEquals(1,违例项.size());//此处失败,表示没有违例项 },spring,hibernate,Spring,Hibernate,正如您所看到的,从数据库中创建并加载了mymodel,其中persons为空,所以请尝试对ServerGroup“group”运行验证,但没有冲突,如果我将其更改为“急切加载”,那么它会工作吗?请提供任何建议。您使用的是哪个版本的hibernate?hibernate版本4.1.4.Final和hibernate验证版本4.3.0.Final。您是否也可以将@NotEmpty注释添加到persons属性以查看其功能?问题已编辑。尝试@NotEmpty(groups=ServerGroup.cla

正如您所看到的,从数据库中创建并加载了mymodel,其中persons为空,所以请尝试对ServerGroup“group”运行验证,但没有冲突,如果我将其更改为“急切加载”,那么它会工作吗?请提供任何建议。

您使用的是哪个版本的hibernate?hibernate版本4.1.4.Final和hibernate验证版本4.3.0.Final。您是否也可以将@NotEmpty注释添加到persons属性以查看其功能?问题已编辑。尝试@NotEmpty(groups=ServerGroup.class)。还是不行!
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
}