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

后端 未结 2 609
情话喂你
情话喂你 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.

    <filter>
        <filter-name>Open Session in View Filter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Open Session in View Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>   
    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    0 讨论(0)
  • 2020-12-18 13:35

    As Error Said

    21:56:35,526 ERROR [org.apache.struts2.dispatcher.Dispatcher] (http-/127.0.0.1:8080-1) Exception occurred during processing request: org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.ast.domain.admin.AtOrganisation.atDivisions, could not initialize proxy - no Session: 
    

    This is because hibernet is not able to fetch data from database due to lazy="false". For that you have have to use

    lazy="false" fetch="join" at atDivisions

    com.ast.domain.admin.AtOrganisation.atDivisions

    0 讨论(0)
提交回复
热议问题