-1

I am working on a spring boot application. I have developed a functionality that works well without error. However, when I did an integration test with the same code I have the following error:

partnerOrderHeaders assoc2s

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: package.Object1.assoc2s, could not initialize proxy - no Session

Here is the method in which there is the error

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;

}

The error occured at this line

   for (Assoc1 Assoc1 : Assoc1s){

But the important thing is that the functionality works without error while it does not work in the integration test part. I think that I miss a configuration set up in the test part. Could you help me please

As Nick asked me, I add test code

    @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));  
}
flamant
  • 733
  • 4
  • 15
  • 41

2 Answers2

0

You should set FetchType.EAGER in OneToMany relationship DimPartner with FactAssocPartnerOrderHeader

Nick
  • 805
  • 5
  • 14
  • Hi Nick, I already thought about it, but for the need of the application, I must keep LAZY loading when I run the functionality. As I mentioned the error occurs only in the test part. There should be another fix, because it runs perfectly when I hit the API from the browser and lazy loading is taken into account. What is the difference with the test part ? I think there is a workaround – flamant Dec 31 '19 at 09:39
  • Ok. Try to add string Hibernate.initialize(dimPartner.getPartnerOrderHeaders()); – Nick Dec 31 '19 at 09:41
  • Hi Nick, the result is the same. The same error is mentioned at the line added, that is Hibernate.initialize(dimPartner.getPartnerOrderHeaders()); – flamant Dec 31 '19 at 13:36
  • I add that in the integration test, I have an in-memory database – flamant Dec 31 '19 at 13:38
0

I finally found the solution : add @Transactional annotation on the test class. Thanks for your replies

flamant
  • 733
  • 4
  • 15
  • 41