Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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 异常:org.hibernate.LazyInitializationException:延迟初始化角色集合失败_Spring_Spring Boot_Integration Testing_Lazy Initialization - Fatal编程技术网

Spring 异常:org.hibernate.LazyInitializationException:延迟初始化角色集合失败

Spring 异常:org.hibernate.LazyInitializationException:延迟初始化角色集合失败,spring,spring-boot,integration-testing,lazy-initialization,Spring,Spring Boot,Integration Testing,Lazy Initialization,我正在开发一个spring启动应用程序。我开发了一个功能,可以正常工作,没有错误。但是,当我使用相同的代码进行集成测试时,出现以下错误: partnerOrderHeaders assoc2s org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: package.Object1.assoc2s, could not initialize proxy - no Ses

我正在开发一个spring启动应用程序。我开发了一个功能,可以正常工作,没有错误。但是,当我使用相同的代码进行集成测试时,出现以下错误:

partnerOrderHeaders assoc2s

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: package.Object1.assoc2s, could not initialize proxy - no Session
以下是存在错误的方法

private  List<ObjectClusterData> constructObjectClusterDataFromObject1(Object1 Object1) {
    List<ObjectClusterData> ObjectClusterDatas = new ArrayList<ObjectClusterData>();
    ObjectClusterData ObjectClusterData = new ObjectClusterData();
    Set<Assoc1> Assoc1s = Object1.getPartnerOrderHeaders();
    Object2 Object2 = null;
    Set<Object2> Object2s = null;
    ObjectItemClusterData ObjectItemClusterData = null;
    for (Assoc1 Assoc1 : Assoc1s) {
        ObjectClusterData = new ObjectClusterData();
        Object2 = Assoc1.getHeader();
        ObjectClusterData.setNum(Object2.getNum());
        PartData partData = Object1 != null ? ConvertDomainObjectToDataObjectAndReverse.convertObject1ToPartData(Object1) : null;
        ObjectClusterData.setPartDatas(Arrays.asList(new PartData[]{partData}));
        Object2s = Object2.getItems();
        List<ObjectItemClusterData> ObjectItemClusterDatas = new ArrayList<ObjectItemClusterData>();
        for (Object2 Object2 : Object2s) {
            ObjectItemClusterData = new ObjectItemClusterData();
            ObjectItemClusterData.setProperty1(Object2.getProperty1());
            ObjectItemClusterData.setProperty2(Object2.getProperty2());
            ObjectItemClusterData.setProperty3(Object2.getProperty3());
            ObjectItemClusterData.setDimData(Object2.getDim() != null ? ConvertDomainObjectToDataObjectAndReverse.convertDimObjectToDimObjectData(Object2.getDim()) : null);
            ObjectItemClusterData.setMat(Object2.getMaterials() != null ? ConvertDomainObjectToDataObjectAndReverse.convertDimMatObjectToMatObjecData(Object2.getMat()) : null);
            ObjectItemClusterDatas.add(ObjectItemClusterData);
        }
        ObjectClusterData.setObject2s(ObjectItemClusterDatas);
        ObjectClusterDatas.add(ObjectClusterData);
    }
    return ObjectClusterDatas;

}
但重要的是,当功能在集成测试部分不工作时,它可以正常工作。我想我错过了在测试部分设置的配置。你能帮我吗

正如Nick问我的,我添加了测试代码

    @Test
    @WithMockUser(username = LOGIN, password = PASSWRD, roles = ROLE)
    public void testGetOrderReferenceByOrderWithNoError() throws Exception {

        // getInsertAndUpdateModificationInDateWindow
          restCustomerOrderMockMvc
      .perform(get("/api/orders/modification?"
              +"beginInsertionOrModificationDate=" + ORDER_ZONE_DATE_TIME_NO_1+
              "&endInsertionOrModificationDate=" + ORDER_ZONE_DATE_TIME_NO_4)
      .accept(MediaType.APPLICATION_JSON))
      .andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
      .andExpect(jsonPath("$.resultCode").value(BusinessErrorCode.NO_ERROR.getCode()))
      .andExpect(jsonPath("$.size").value("1"))
      .andExpect(jsonPath("$.entities[0]." + F_OBJECT1_NUM).value(NUM_1))                
      .andExpect(jsonPath("$.entities[0].partDatas[0]" + F_USER_COM_API).value(PART_1))
      .andExpect(jsonPath("$.entities[0].Object2s[0]" + F_ITEM_NO).value(ITEM_NO_1))
      .andExpect(jsonPath("$.entities[0].Object2s[0].dimensionData." + F_WEIGHT.value(WEIGHT))
      .andExpect(jsonPath("$.entities[0].Object2s[0].materialData." + F_GRADE).value(GRADE_1));  
}

您应该使用FactAssocPartnerOrderHeader在OneToMany关系DimPartner中设置FetchType.EAGER

我终于找到了解决方案:在测试类上添加@Transactional注释。感谢您的回复

请添加测试代码Hi-Nick,我添加了测试代码Hi-Nick,我已经考虑过了,但是为了应用程序的需要,我必须在运行功能时保持延迟加载。正如我提到的,错误只发生在测试部分。应该还有另一个修复程序,因为当我从浏览器点击API时,它可以完美运行,并且考虑了延迟加载。测试部件有什么区别?我认为有一个解决办法。尝试添加字符串Hibernate.initialize(dimPartner.getPartnerOrderHeaders());嗨,尼克,结果是一样的。添加的行中提到了相同的错误,即Hibernate.initialize(dimPartner.getPartnerOrderHeaders());在集成测试中,我添加了一个内存数据库,理想情况下,您不应该在测试类上添加事务。您的代码应该管理事务,而不是测试类。
    @Test
    @WithMockUser(username = LOGIN, password = PASSWRD, roles = ROLE)
    public void testGetOrderReferenceByOrderWithNoError() throws Exception {

        // getInsertAndUpdateModificationInDateWindow
          restCustomerOrderMockMvc
      .perform(get("/api/orders/modification?"
              +"beginInsertionOrModificationDate=" + ORDER_ZONE_DATE_TIME_NO_1+
              "&endInsertionOrModificationDate=" + ORDER_ZONE_DATE_TIME_NO_4)
      .accept(MediaType.APPLICATION_JSON))
      .andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
      .andExpect(jsonPath("$.resultCode").value(BusinessErrorCode.NO_ERROR.getCode()))
      .andExpect(jsonPath("$.size").value("1"))
      .andExpect(jsonPath("$.entities[0]." + F_OBJECT1_NUM).value(NUM_1))                
      .andExpect(jsonPath("$.entities[0].partDatas[0]" + F_USER_COM_API).value(PART_1))
      .andExpect(jsonPath("$.entities[0].Object2s[0]" + F_ITEM_NO).value(ITEM_NO_1))
      .andExpect(jsonPath("$.entities[0].Object2s[0].dimensionData." + F_WEIGHT.value(WEIGHT))
      .andExpect(jsonPath("$.entities[0].Object2s[0].materialData." + F_GRADE).value(GRADE_1));  
}