복붙노트

[SPRING] JSONException : org.hibernate.LazyInitializationException Spring struts에서 최대 절전 모드 응용 프로그램

SPRING

JSONException : org.hibernate.LazyInitializationException Spring struts에서 최대 절전 모드 응용 프로그램

위의 설명 오류가 발생하고 일부 연구를 수행 한 결과 org.springframework.orm.hibernate4.support.OpenSessionInViewFilter가 해결책이라는 것을 알았습니다. 하지만 그 응용 프로그램과 함께 작동하지 않습니다. jqGrid에 데이터가로드되는 동안 오류가 발생합니다.

Struts2 액션 클래스 :

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



}

서비스 레이어 get org 메소드 :

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

}

을 포함한다.

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

해결법

  1. ==============================

    1.그리드에 표시하려는 특성 만 JSON에 직렬화해야합니다. 그것은 다른 방법으로 달성 될 수 있습니다 :

    그리드에 표시하려는 특성 만 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
    

    View Session In View 필터를 사용했다면 필요에 따라 즉, 직렬화 중에 속성을로드하고 초기화 할 수 있습니다. 액션이 JSON 결과를 반환 할 때 최대 절전 모드 세션이 여전히 열려 있기 때문입니다. result가 반환되면 Struts2가 결과를 반환합니다. 이 시점에서 세션이 열려 있어야합니다. 이를 가능하게하려면 필터 체인의 순서를 구성해야합니다. View 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>
    
  2. ==============================

    2.오류가 있다고 말함

    오류가 있다고 말함

    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

  3. from https://stackoverflow.com/questions/23982290/jsonexception-org-hibernate-lazyinitializationexception-in-spring-struts-hibern by cc-by-sa and MIT license