About the exception that is thrown when I use ajax to request servletlist to convert JSON. There is a cycle in the hierarchy! And Converting list to jsonarray to solve the problem!

Source: Internet
Author: User

Today, when I completed my own online bookstore project, I was ready to use ajax to perform operations when I checked the book comments. (previously, I refreshed all the pages directly and felt uncomfortable. I just learned Ajax, so I think Ajax is used). The backend uses hibernate to operate databases. However, when the Code test is completed, an error is reported.

Then Baidu Google looked for a solution and found that most of them said that the query resultsObjects are placed in the list set. Then, the objects in the list are associated with the database through foreign keys, that is, there are multiple to one or one pair in the object model.Multi-Level Association,
In this way, the list set is associated with an object instead of a string or number, resulting in an internal endless loop when jsonarray. fromobject () occurs. Finally, it was confirmed that this was the cause. Most of the solutions on the Internet are about removing the attributes of associated objects and then filtering out the ID attributes */
Jsonconfig config1 = new jsonconfig ();
/* Use arrays to write out the items to be filtered */
Config1.setexcludes (New String [] {"user "});
Jsonobject p1 = jsonobject. fromobject (Review, config1 );
The first parameter is put into the object you want to convert, list and so on. The second parameter is put into the jsonconfig object.
After thinking a little bit, I found that, if the field you want to query includes the attribute values in the associated object, what should I do? In this way, you will not be able to retrieve it on the interface after it is not converted. So it does not seem to meet my requirements.
At last, I felt that I could write a JavaBean for the database table without object association, as I did when using JDBC,
 
After traversing the list of data read from the database, retrieve all the values and assign values to the newly created JavaBean object one by one, and create a new list, used to hold the changed object. Then, use jsonarray. formobject (list) to add the newly created list set. The conversion is successful. No error is reported if the operation is successful.

Of course, this method is enough for me. After all, I am still a newbie. It is okay to solve the problem. I don't know if there will be any improvement in Strut in the future? I have just finished learning javaweb, and Hibernate is all self-taught. Ajax has just finished learning, so if you see this, don't say that you are not solving it.

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.