JSONException: org.hibernate.LazyInitializationException In Spring struts hibernate application

后端 未结 2 615
情话喂你
情话喂你 2020-12-18 12:48

I\'m getting above explain error and done some research and found that org.springframework.orm.hibernate4.support.OpenSessionInViewFilter is solution. But its n

2条回答
  •  太阳男子
    2020-12-18 13:30

    You should serialize to JSON only those properties that you want to show in the grid. It could be achieved by different ways:

    1) Limit properties you want to serialize using parameters to json result

    @Result(type = "json", params = {
           "includeProperties", "gridModel\\[\\d+\\]\\.id, gridModel\\[\\d+\\]\\.name, total, records, rows, page, sidx, searchField, searchString", 
       "excludeNullProperties", "true"
    })
    

    2) Exclude properties via json annotation.

    @JSON(serialize = false, deserialize = false) // this prevents from output name field values
    

    If you have used Open Session In View filter, then it can load those properties and initialize them on demand, i.e. during serialization. Because the hibernate session still remains open when your action returns JSON result. When result is returned Struts2 executes it. At this moment the session should be open. To make this possible you should configure the order of the filter chain. If Open Session In View filter is in the first order then is possible to close the session after Struts2 result is executed.

    
        Open Session in View Filter
        
            org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
        
    
    
        Open Session in View Filter
        /*
       
    
    
        struts2
        
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        
    
    
        struts2
        /*
    
    

提交回复
热议问题