Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

json - JSONException: org.hibernate.LazyInitializationException in Spring, Struts 2, Hibernate application

I'm getting above explain error and done some research and found that org.springframework.orm.hibernate4.support.OpenSessionInViewFilter is solution. But its not works with my application.

Error occurs during data load into jqGrid.

Struts2 Action class :

import com.ast.domain.admin.AtDivision;
import com.ast.domain.admin.AtOrganisation;
import com.ast.service.admin.OrganisationBo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.ArrayList;
import java.util.List;

public class OraganisationAction extends ActionSupport implements ModelDriven<AtOrganisation> {

    private String sidx;
    // Search Field
    private String searchField;
    // The Search String
    private String searchString;
    // he Search Operation ['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']
    private String searchOper;
    // Your Total Pages
    private Integer total = 0;
    //Your result List
    //private List<MyBean> gridModel;
    //get how many rows we want to have into the grid - rowNum attribute in the grid
    private Integer rows = 5;
    //Get the requested page. By default grid sets this to 1.
    private Integer page = 0;
    // sorting order - asc or desc
    private String sord;
    private boolean loadonce = false;
    private Integer records = 0;

    private AtOrganisation org = new AtOrganisation();
    private AtDivision div = new AtDivision();
    private List<AtOrganisation> gridModel = new ArrayList<AtOrganisation>();
    private List<AtOrganisation> temp_gridModel = new ArrayList<AtOrganisation>();

    private OrganisationBo orgBo;

    private String aoId;

    public List<AtOrganisation> getTemp_gridModel() {
        return temp_gridModel;
    }

    public void setTemp_gridModel(List<AtOrganisation> temp_gridModel) {
        this.temp_gridModel = temp_gridModel;
    }

    public Integer getRecords() {
        return records;
    }

  public void setRecords(Integer records) {
        this.records = records;
        if (this.records > 0 && this.rows > 0) {
            this.total = (int) Math.ceil((double) this.records / (double) this.rows);
        } else {
            this.total = 0;
        }
    }

    public String getSidx() {
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    public String getSearchField() {
        return searchField;
    }

    public void setSearchField(String searchField) {
        this.searchField = searchField;
    }

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }

    public String getSearchOper() {
        return searchOper;
    }

    public void setSearchOper(String searchOper) {
        this.searchOper = searchOper;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public String getSord() {
        return sord;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public boolean isLoadonce() {
        return loadonce;
    }

    public void setLoadonce(boolean loadonce) {
        this.loadonce = loadonce;
    }

    public List<AtOrganisation> getGridModel() {
        return gridModel;
    }

    public void setGridModel(List<AtOrganisation> gridModel) {
        this.gridModel = gridModel;
    }

    public String getAoId() {
        return aoId;
    }

    public void setAoId(String aoId) {
        this.aoId = aoId;
    }

    public void setOrgBo(OrganisationBo orgBo) {

        this.orgBo = orgBo;
    }

    public OrganisationBo getOrgBo() {
        return orgBo;
    }

    public AtDivision getDiv() {
        return div;
    }

    public void setDiv(AtDivision div) {
        this.div = div;
    }

    public AtOrganisation getOrg() {
        return org;
    }

    public void setOrg(AtOrganisation org) {

        this.org = org;
    }

    @Override
    public AtOrganisation getModel() {
        return org;
    }

    public String gridaction() {
        temp_gridModel = orgBo.showorg();
        System.out.println("setRecords : "+temp_gridModel.size());
        setRecords(temp_gridModel.size());
        int to = (getRows() * getPage());
        int from = to - getRows();

        if (to > getRecords()) {
            to = getRecords();
        }
        if (loadonce) {
            setGridModel(temp_gridModel);
        } else {
            if (searchString != null && searchOper != null && !searchString.equals("") && !searchOper.equals("")) {
                System.out.println("Searching within Database");
                if (searchOper.equalsIgnoreCase("cn")) {
                    // setGridModel(DAO.findbyName(searchString));
                }
            } else {
                System.out.println("Not Searching Anywhere");
                //setGridModel(DAO.find(from, to));
                System.out.println("dili 4444");
                setGridModel(temp_gridModel);
            }
        }
        System.out.println("getRecords() : "+(double) getRecords());
            System.out.println("(double) getRows() : "+(double) getRows());
        total = (int) Math.ceil((double) getRecords() / (double) getRows());
        System.out.println("tot " + total);
        System.out.println(gridModel.size());
        return "grid";
    }

    public String add() throws Exception {
        orgBo.addOrg(org);
        gridaction();
        return SUCCESS;

    }

    public String delete() throws Exception {

        orgBo.deleteOrg(org);
        gridaction();
        return SUCCESS;
    }

    public String edit() throws Exception {

        orgBo.updateOrg(org);
        gridaction();
        return SUCCESS;
    }



}

Service layer get org method :

  @Override
@Transactional(propagation = Propagation.REQUIRED)
public List showorg() {

    return orgDao.showorg();
}

DAO method :

  @Override
public List showorg() {

Session session = sessionfactory.getCurrentSession();

org.hibernate.Query query = session.createQuery("from AtOrganisation");

List list = query.list();

return list;

}

web.xml:

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/SpringConfiguration.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
        <param-value>/WEB-INF/tiles.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>

    <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>

    <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>

struts.xml :

<interceptors>
    <interceptor-stack name="defaultStack">
        <interceptor-ref name="params">
           
            <param name="acceptParamNames">
                (\[\d+\]\.)*\w+((\.\w+)|(\[\d+\])|(\(\d+\))|(\['\w+'\])|(\('\w+'\)))*
            </param>
        </interceptor-ref>
    </interceptor-stack>
</interceptors>
<action name="*orgs" class="com.ast.action.admin.OraganisationAction"
        method="{1}">
    <interceptor-ref name="defaultStack"/>

    <result name="grid" type="json"></result>   
</action>

Error Stack :

  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: org.apache.struts2.json.JSONException: 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
    at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:246) [struts2-json-plugin-2.3.16.3.jar:2.3.16.3]
    at org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178) [struts2-json-plugin-2.3.16.3.jar:2.3.16.3]
    at org.apache.struts2.json.JSONWriter.process(JSONWriter.java:168) [struts2-json-plugin-2.3.16.3.jar:2.3.16.3]
    at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134) [struts2-json-plugin-2.3.16.3.jar:2.3.16.3]
    at org.apache.struts2.json.JSONWriter.write(JSONWriter.java:102) [struts2-json-plugin-2.3.16.3.jar:2.3.16.3]
    at org.apache.struts2.json.JSONUtil.serialize(JSONUtil.java:116) [struts2-json-plugin-2.3.16.3.jar:2.3.16.3]
    at org.apache.struts2.json.JSONResult.createJSONString(JSONResult.java:202) [struts2-json-plugin-2.3.16.3.jar:2.3.16.3]
    at org.apache.struts2.json.JSONResult.execute(JSONResult.java:176) [struts2-json-plugin-2.3.16.3.jar:2.3.16.3]
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:371) [xwork-core-2.3.16.jar:2.3.16]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:275) [xwork-core-2.3.16.jar:2.3.16]
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) [xwork-core-2.3.16.jar:2.3.16]
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [xwork-core-2.3.16.jar:2.3.16]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.16.jar:2.3.16]
    at org.apache.struts2.impl.StrutsActionProxy.execute(S

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...