The Java Hibernate Association query returns a collection that is converted to JSON report JSON dead loop processing

Source: Internet
Author: User



Public String User (HttpServletRequest request) {
Set filters first, only the objects you want
Jsonconfig config = new Jsonconfig ();
Config.setjsonpropertyfilter (New PropertyFilter () {
public Boolean apply (object arg0, String arg1, object arg2) {
if (Arg1.equals ("Tspdepart")) {
return true;
} else {
return false;
}
}
});
Prepare data for drop-down boxes in departmental query criteria
list<tsdepart> departlist = systemservice.getlist (Tsdepart.class);
Request.setattribute ("Departlist", Jsonarray.fromobject (Departlist,config). toString ());
Request.setattribute ("Departsreplace", Roletojson.listtoreplacestr (Departlist, "Departname", "id"));
return "System/user/userlist";
}



var replacedata=${departlist};

for (k=0;k<replacedata.length;k++) {
var r=replacedata[k];
if (r.id = = value) {
return r.departname;
}
}


Refer to: http://www.blogjava.net/peiliangye/articles/372062.html

When jquery calls JSON, Net.sf.json.JSONException:There is a cycle in the hierarchy!

Encountered some problems, such as hibernate delay loading error, this is the old-fashioned problem, a look on the lazy=flase will be OK to add a. Unexpectedly soon completed and encountered a new problem, json dead loop, really frustrating. Exceptions are as follows:


Net.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:674)
At Net.sf.json.JSONObject.fromObject (jsonobject.java:181)
At Net.sf.json.jsonarray._processvalue (jsonarray.java:2381)
At Net.sf.json.JSONArray.processValue (jsonarray.java:2412)
truncated. See log file for complete StackTrace
>


A careful look at the discovery is hibernate the main foreign key association of the error, and then think under the JSON source code, found that the big cost of all the trouble did not get the JSON source, or the old-fashioned anti-compilation look, found that jsonarray according to the different types of judgment to call the corresponding method,

If (object instanceof Collection)
Return _fromcollection ((Collection) object, jsonconfig);

What I get from hibernate is list, so I call the _fromcollection method, and the method inside finds a problem: The method will constantly disassemble the entity attribute until it is not, and my contactgroup has two properties for its own association

Private Set contactgroups = new HashSet (0);
Private Set contactgrouppersons = new HashSet (0);


That is, the primary foreign key itself is associated with a dead loop, then how can not let him appear this situation, there should be a configuration of the parameters of the latter to terminate the cycle of the place, see the hair
Now, Jsonconfig, oh, config should be configuration parameters bar, see Jsonconfig saw the properties, a bit dizzy PropertyFilter
, did not mention, looked long time, found a property propertyfilter,propertyfilter is a interface, the code is as follows:


public interface PropertyFilter
{


Public abstract Boolean apply (Object obj, String S, Object obj1);
}


That is, I can filter out the corresponding property in the list by this method, just let it return true to filter out, ..., a little hanging ... Let's rewrite this method:


Jsonconfig cfg = new Jsonconfig ();
Cfg.setjsonpropertyfilter (New PropertyFilter ()
{
public Boolean apply (object source, String name, object value) {
if (Name.equals ("contactgroups") | | Name.equals ("Contactgrouppersons")) {
return true;
} else {
return false;
}
}
});

The contactgroups and contactgrouppersons in the entity bean generated by hibernate are filtered out, OK!

Then call Jsonarray.fromobject (MYCHILDREN,CFG); Mychildren is the list returned by Hibernate.

1650) this.width=650; "src=" Http://www.blogjava.net/Images/OutliningIndicators/None.gif "align=" Top "/>List<ShoppingCart>Listcarts=Scartserviceimpl
2650) this.width=650; "src=" Http://www.blogjava.net/Images/OutliningIndicators/None.gif "align=" Top "/> . Shoppingcarttable (ShoppingCart);
3650) this.width=650; "src=" Http://www.blogjava.net/Images/OutliningIndicators/None.gif "align=" Top "/> //First, filter the disassembly of set set
4650) this.width=650; "src=" Http://www.blogjava.net/Images/OutliningIndicators/None.gif "align=" Top "/>jsonconfig config= NewJsonconfig ();
5650) this.width=650; "id=" Codehighlighter1_204_405_open_image "src=" http://www.blogjava.net/Images/ Outliningindicators/expandedblockstart.gif "align=" Top "alt=" Expandedblockstart.gif "/> config.setJsonPr Opertyfilter (NewPropertyFilter (){
6650) this.width=650; "src=" Http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif "align=" Top "/> @Override
7650) this.width=650; "id=" Codehighlighter1_286_399_open_image "src=" http://www.blogjava.net/Images/ Outliningindicators/expandedsubblockstart.gif "align=" Top "alt=" Expandedsubblockstart.gif "/> Public Booleanapply (Object arg0, String arg1, object arg2){
8650) this.width=650; "id=" Codehighlighter1_328_356_open_image "src=" http://www.blogjava.net/Images/ Outliningindicators/expandedsubblockstart.gif "align=" Top "alt=" Expandedsubblockstart.gif "/> if(Arg1.equals ("Shoppingcarts")) {
9650) this.width=650; "src=" Http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif "align=" Top "/> return true;
Ten650) this.width=650; "id=" Codehighlighter1_363_392_open_image "src=" http://www.blogjava.net/Images/ Outliningindicators/expandedsubblockstart.gif "align=" Top "alt=" Expandedsubblockstart.gif "/>}< /c0> Else {
11 650 "this.width=650;" src= "http://www.blogjava.net/Images/ Outliningindicators/inblock.gif "align=" Top "/>                              &NBSP; false 12
-650) this.width=650; "src=" Http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif "align= "Top"/>}
-650) this.width=650; "src=" Http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif "align=" Top "/>});
the650) this.width=650; "src=" Http://www.blogjava.net/Images/OutliningIndicators/None.gif "align=" Top "/> //convert data to JSON data
-650) this.width=650; "src=" Http://www.blogjava.net/Images/OutliningIndicators/None.gif "align=" Top "/>Jsonarray Jsonobject=jsonarray.fromobject (listcarts, config);
-650) this.width=650; "src=" Http://www.blogjava.net/Images/OutliningIndicators/None.gif "align=" Top "/> System.out.println (Jsonobject.tostring ());
-650) this.width=650; "src=" Http://www.blogjava.net/Images/OutliningIndicators/None.gif "align=" Top "/>


The Java Hibernate Association query returns a collection that is converted to JSON report JSON dead loop processing

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.