Conversion between pojo and JSON of hibernate cascading ing

Source: Internet
Author: User

Today, we have found a new way to solve this problem. The cascading relationship of hibernate solves a lot of troubles, but in this case it is troublesome. Next, let's talk about the solution: separating cascading classes, specifically separating DTO with ing attributes and the DTO mapped with cascading relationships, then place the separated DTO in the map, so that there will be no problem during conversion using the JSON-lib tool!

For example

Role = This. roleservice. searchrole (ID); role r = new role (); R. setrname (role. getrname (); R. setrdescribe (R. getrdescribe (); List <limits> Limits = new arraylist <limits> (); limits. addall (role. getlimits (); Map <string, Object> map = new hashmap <string, Object> (); map. put ("role", R); map. put ("Limits", limits); jsonobject Jo = jsonobject. fromobject (MAP );

(The content above is the description part and a supplement to the shortcomings of the Solution Below)


Today, I encountered such an error inexplicably: net. SF. JSON. jsonexception: there is a cycle in .... I know what it means, but I did not think about it because I just used JSON. I checked it online. The reason is: The Relationship ing of hibernate will query a series of objects related to it during cascade queries (of course, this is the ing relationship between objects configured ), JSON-lib is not mature after all, so data in this form cannot be parsed into JSON objects, but it also provides a solution as mentioned below. But this Toolkit is enough. Now we can directly convert the list, MAP, pojo object, and so on into a JSON object!

I spoke a little nonsense, but it was just a bit excited because I had just known JSON. The specific solution is as follows:

Jsonconfig Config = new jsonconfig (); config. setjsonpropertyfilter (New propertyfilter () {public Boolean apply (Object arg0, string arg1, object arg2) {If (arg1.equals ("user") | arg1.equals ("submessages ")) {return true ;}else {return false ;}}});

Here I will explain this sectionCodeIt provides a filtering function. If an associated object is encountered, it automatically filters out and does not execute the object associated with the Association. It may not be easy to understand this code. I posted the code for configuring link ing in hibernate to help you understand it:

<! -- Configure the relationship between users and topic posts --> <role-to-one name = "user" class = "bbs. Po. User" column = "user_id"/> <! -- Configure the relationship between the topic post and the reply post --> <set name = "submessages" table = "sub_message" inverse = "true" cascade = "all" lazy = "false" order-by = "date ASC"> <key column = "theme_id"/> <one-to-learn class = "BBS. po. submessage "/> </set>

This class is configured with many-to-one pairs with users and one-to-many pairs with submessage. During the query, a series of non-class objects are associated and queried, resulting in a JSON-lib conversion failure.

Next I will introduce the function of the JSON-lib class in the example: it specifically converts pojo to a JSON object or converts a JSON object to a pojo. This is its usage:

1. jsonarray submsgs = jsonarray. fromobject (submessages, config); config is optional. You can configure the config parameter in the preceding case. You can directly use the fromobject (OBJ) method without the above requirement, it converts the data in the standard JSON object format as follows:

{["ATTR", "content",...},...]}

2. jsonobject jtmsg = jsonobject. fromobject (themessage, config); this is specially used to parse standard pojo, or map objects. The format of pojo objects is needless to say. The form of map is {"str ", "str "}. Of course, this is some of the usage I have used, and I have not gone into depth, so I am sorry to give you a lot of advice !!

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: 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.