Spring-data-jpa -- if one-to-one and others-to-one annotations are used, an error solution will appear during Jackson's json String Conversion,

Source: Internet
Author: User

Spring-data-jpa -- if one-to-one and others-to-one annotations are used, an error solution will appear during Jackson's json String Conversion,

References:

Http://blog.csdn.net/remote_roamer/article/details/51330843

Http://blog.csdn.net/xiaodaiye/article/details/51118870

In spring-data-jpa, one-to-one and two-to-one annotations are used to produce json strings.

Find the following solutions after reading the information:

Add @ JsonManagedReference to the pojo of the master table to annotate the joined fields:

@ Onetovel (cascade = CascadeType. REFRESH, mappedBy = "ruleType", targetEntity = Rule. class) @ JsonManagedReferenceprivate Set rule;

Add @ JsonBackReference to the pojo of the sub-table to annotate associated fields.

@ ManyToOne (cascade = CascadeType. REFRESH, fetch = FetchType. EAGER) @ JsonBackReference @ JoinColumn (name = "TYPE_ID") private RuleType ruleType;

Then, Jackson is used to generate json

New ObjectMapper (). writeValueAsString (obj );

 

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.