JSONException:org.hibernate.LazyInitializationException在Spring struts中使用hibernate应用程序

时间:2014-06-01 17:03:37

标签: json spring hibernate struts2 jqgrid

我上面解释错误并做了一些研究,发现org.springframework.orm.hibernate4.support.OpenSessionInViewFilter是解决方案。但它不适用于我的应用程序。 数据加载到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;
    }



}

服务层获取组织方法:

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

    return orgDao.showorg();
}

DAO方法:

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

错误堆栈:

  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(StrutsActionProxy.java:54) [struts2-core-2.3.16.jar:2.3.16]
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562) [struts2-core-2.3.16.jar:2.3.16]
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) [struts2-core-2.3.16.jar:2.3.16]
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99) [struts2-core-2.3.16.jar:2.3.16]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
    at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_40]
Caused by: 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
Please give some way to over come this issue.

2 个答案:

答案 0 :(得分:1)

您应该仅将要在网格中显示的属性序列化为JSON。它可以通过不同的方式实现:

1)使用参数将要序列化的属性限制为json结果

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

2)通过json注释排除属性。

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

如果您已使用Open Session In View过滤器,则它可以加载这些属性并按需初始化它们,即在序列化期间。因为当您的操作返回JSON结果时,hibernate会话仍然保持打开状态。返回结果时,Struts2会执行它。此时会议应该开放。为了实现这一点,您应该配置过滤器链的顺序。如果Open Session In View过滤器是第一个顺序,则可以在执行Struts2结果后关闭会话。

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

答案 1 :(得分:0)

正如错误所说

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: 

这是因为hibernet由于lazy="false"而无法从数据库中获取数据。 为此你必须使用

lazy="false" fetch="join"atDivisions

com.ast.domain.admin.AtOrganisation.atDivisions