In hibernate, how does one solve the problem of converting objects to json endless loops in multiple relationships? hibernatejson

Source: Internet
Author: User

In hibernate, how does one solve the problem of converting objects to json endless loops in multiple relationships? hibernatejson

Write down the reason first! I am writing an SSH project. The problem I encountered in the project is to convert the paging object (that is, the pageBean object) into json data. The following code is used:

Public class PageBean <T> {
// Current page
Private int pageNum;
// Page display quantity
Private int pageSize;
// Total number of queries (obtained from database queries)
Private int totalRecord;
// Start indexing on the current page
Private int startIndex;
// Total page count
Private int totalPage;
// Data
Private List <T> data;

Because I wrote the block between the Group and the members, the group and the members

The hibernate relationship is multi-to-Multi-relationship. When you want to convert the group member object set to the jsong format, you should find that the Group object set contains the member object set, A group object is a set of member objects. This is the problem, and it is also an exception.

Freemarker. template. templateModelException: Method public java. lang. string org. apache. commons. lang. exception. nestableRuntimeException. getMessage (int) threw an exception when invoked on net. sf. json. JSONException: There is a cycle in the hierarchy!
At freemarker.ext.beans.SimpleMethodModel.exe c (SimpleMethodModel. java: 130)

Because you want to convert a group object to a json object, the member object is converted to json during conversion, which leads to an endless loop.

What you want to do is to break the damn loop and attach all the code

PageBean <User> resultUser = userService. findUserByUse (evenFind); // query all User group object data and encapsulate it in a paging object
JSONArray jsonArray = new JSONArray ();
JSONObject jsonObject = new JSONObject ();
If (resultUser! = Null ){

// Retrieve each object cyclically and generate a json object. You cannot directly generate a json array object. Otherwise, the original error is reported,
If (resultUser. getData (). size ()> 0 ){
For (int I = 0; I <resultUser. getData (). size (); I ++ ){
JsonConfig jsonConfig = new JsonConfig ();
JsonConfig. setIgnoreDefaultExcludes (true );
JsonConfig. setCycleDetectionStrategy (CycleDetectionStrategy. LENIENT );
// Prevent self-contained
String [] strArry = {"userGounp "};

// You need to remove the attribute names converted to json objects.
JsonConfig. setExcludes (strArry );
JSONObject json = JSONObject. fromObject (resultUser. getData (). get (I), jsonConfig );

// Add to the json object Array
JsonArray. add (json );
}
}
JsonObject. put ("data", jsonArray );
JsonObject. put ("pageSize", resultUser. getPageSize ());
JsonObject. put ("pageNum", resultUser. getPageNum ());
}

For the first time, please forgive me for not writing well. All of them are hand-written and thought-provoking.

 

Related Article

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.