Net. SF. JSON. jsonexception: there is a cycle in the hierarchy! Error Solution

Source: Internet
Author: User

Hibernate is prone to this problem, mainly because of the cascading relationship of pojo class attributes. For example, employees and departments have department attributes in the employee table, and there is an employee set in the department table. In this way, the nested reference problem exists and this exception is thrown.

The solution is simple. You can use the setexcludes function to remove cascading attributes when converting each object into a JSON object, as shown below:

 

1 // get all Departments 2 // return the JSON object string 3 Public String getalldep () {4 List list = deptdao. findall (); 5 jsonconfig Config = new jsonconfig (); 6 config. setexcludes (New String [] {"EMPs"}); // remove the EMPs attribute 7 string JSON = jsonarray. fromobject (list, config ). tostring (); 8 return JSON; 9} 10 11 // get all employees 12 Public String getallemp (int id) {13 list = empdao. findbyproperty ("dept. deptid ", ID); 14 jsonconfig Config = new jsonconfig (); 15 config. setexcludes (New String [] {"Dept"}); // remove the dept attribute 16 string JSON = jsonarray. fromobject (list, config ). tostring (); 17 return JSON; 18}

 

 

The Exception Code is as follows:

Severe: servlet. Service () for servlet 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 $ fef4bb53. 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: 601) 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: 501) 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)

 

 

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.