Jsonexception:there is a cycle in the hierarchy solution

Source: Internet
Author: User

This problem is easy to use with Hibernate, mainly due to the cascading relationship of Pojo class attributes. For example, employees and departments, which have departmental attributes in the employee table, and a collection of employees in the departmental table, will throw this exception if there is a problem with nested references.

The workaround is simple, when you convert each object to a JSON object, use the Setexcludes function to remove the cascading properties, as follows:

Get all Departments    //Return JSON object string public string    getalldep () {        list list = Deptdao.findall ();        Jsonconfig config = new Jsonconfig ();        Config.setexcludes (New string[]{"Emps"});//Remove the Emps attribute        String json = jsonarray.fromobject (list, config). toString () ;        return JSON;    }        Get all employees public    String getallemp (int id) {        list list = Empdao.findbyproperty ("Dept.deptid", id);        Jsonconfig config = new Jsonconfig ();        Config.setexcludes (New string[]{"dept"});//Remove the Dept attribute        String JSON = jsonarray.fromobject (list, config). toString ( );        return JSON;    }



The exception code is as follows:

Severity: Servlet.service () forservlet Springmvc threw Exceptionnet.sf.json.JSONException:There is a cycle in the hierarchy!At Net.sf.json.util.cycledetectionstrategy$strictcycledetectionstrategy.handlerepeatedreferenceasobject ( Cycledetectionstrategy.java:97) at Net.sf.json.jsonobject._frombean (Jsonobject.java:857) at Net.sf.json.JSONObject.fromObject (Jsonobject.java:192) at Net.sf.json.jsonobject._processvalue (Jsonobject.java:2774) at Net.sf.json.jsonobject._setinternal (Jsonobject.java:2798) at Net.sf.json.JSONObject.setValue (Jsonobject.java:1507) at Net.sf.json.jsonobject._frombean (Jsonobject.java:940) at Net.sf.json.JSONObject.fromObject (Jsonobject.java:192) at Net.sf.json.jsonarray._processvalue (Jsonarray.java:2557) at Net.sf.json.JSONArray.processValue (Jsonarray.java:2588) at Net.sf.json.JSONArray.addValue (Jsonarray.java:2575) at Net.sf.json.jsonarray._fromcollection (Jsonarray.java:1082) at Net.sf.json.JSONArray.fromObject (Jsonarray.java:145) at Net.sf.json.jsonobject._processvalue (Jsonobject.java:2749) at Net.sf.json.jsonobject._setinternal (Jsonobject.java:2798) at Net.sf.json.JSONObject.setValue (Jsonobject.java:1507) at Net.sf.json.jsonobject._frombean (Jsonobject.java:940) at Net.sf.json.JSONObject.fromObject (Jsonobject.java:192) at Net.sf.json.jsonarray._processvalue (Jsonarray.java:2557) at Net.sf.json.JSONArray.processValue (Jsonarray.java:2588) at Net.sf.json.JSONArray.addValue (Jsonarray.java:2575) at Net.sf.json.jsonarray._fromcollection (Jsonarray.java:1082) at Net.sf.json.JSONArray.fromObject (Jsonarray.java:145) at Com.service.EmpService.getAllDep (Empservice.java:31) at com.service.empservice$ $FastClassByCGLIB $ $fef 4bb53.invoke (<generated>) at Net.sf.cglib.proxy.MethodProxy.invoke (Methodproxy.java:149) at Org.springframework.aop.framework.cglib2aopproxy$cglibmethodinvocation.invokejoinpoint ( Cglib2aopproxy.java:700) at Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (Reflectivemethodinvocation.java: 149) at Org.springframework.transaction.interceptor.TransactionInterceptor.invoke (Transactioninterceptor.java: 106) at Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (Reflectivemethodinvocation.java: 171) at Org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke (Exposeinvocationinterceptor.java :89) at Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (Reflectivemethodinvocation.java: 171) at Org.springframework.aop.framework.cglib2aopproxy$dynamicadvisedinterceptor.intercept ( Cglib2aopproxy.java:635) at com.service.empservice$ $EnhancerByCGLIB $$8FCE1D77.GETALLDEP (<generated>) at Com.action.DepAction.getAllDept (Depaction.java:22) at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke (Nativemethodaccessorimpl.java:57) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Delegatingmethodaccessorimpl.java:43) at Java.lang.reflect.Method.invoke (Method.java:40s) at Org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod ( Handlermethodinvoker.java:413) at Org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod ( Handlermethodinvoker.java:134) at Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod ( Annotationmethodhandleradapter.java:310) at Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle ( Annotationmethodhandleradapter.java:297) at Org.springframework.web.servlet.DispatcherServlet.doDispatch (Dispatcherservlet.java:875) at Org.springframework.web.servlet.DispatcherServlet.doService (Dispatcherservlet.java:809) at Org.springframework.web.servlet.FrameworkServlet.processRequest (Frameworkservlet.java:571) at Org.springframework.web.servlet.FrameworkServlet.doGet (Frameworkservlet.java:30w) at Javax.servlet.http.HttpServlet.service (Httpservlet.java:690) at Javax.servlet.http.HttpServlet.service (Httpservlet.java:803) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (Applicationfilterchain.java:290) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (Applicationfilterchain.java:206) at Org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal ( Opensessioninviewfilter.java:198) at Org.springframework.web.filter.OncePerRequestFilter.doFilter (Onceperrequestfilter.java:76) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (Applicationfilterchain.java:235) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (Applicationfilterchain.java:206) at Org.apache.catalina.core.StandardWrapperValve.invoke (Standardwrappervalve.java:230) at Org.apache.catalina.core.StandardContextValve.invoke (Standardcontextvalve.java:175) at Org.apache.catalina.core.StandardHostValve.invoke (Standardhostvalve.java:128) at Org.apache.catalina.valves.ErrorReportValve.invoke (Errorreportvalve.java:104) at Org.apache.catalina.core.StandardEngineValve.invoke (Standardenginevalve.java:109) at Org.apache.catalina.connector.CoyoteAdapter.service (Coyoteadapter.java:261) at Org.apache.coyote.http11.Http11Processor.process (Http11processor.java:844) at Org.apache.coyote.http11.http11protocol$http11connectionhandler.process (Http11protocol.java:581) at Org.apache.tomcat.util.net.jioendpoint$worker.run (Jioendpoint.java:447) at Java.lang.Thread.run (Thread.java:722)

I like, driving the code in the lightning to create the perfect! I like to manipulate the code to experience life with what you want! I like to write code in the era of the tide to complete the classic! Every new piece of code in my hands was born to me as a moment to watch the flower of the move! Welcome to share and reprint

Jsonexception:there is a cycle in the hierarchy solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.