1. Prohibit the use of lombok's @Data annotation
After using the @Data annotation, the parent class's toString() method, hashcode() and other methods will be rewritten by default. When saving in the map, the subscript will be calculated according to the equals and hashcode methods, and if @Data annotated When a class has attributes associated with other classes (such as @onetoone, @onetomany, etc.) and the associated attributes are not empty, it will continue to search from the attributes of the related party, and then look for the @Data annotated class from the related party, and then loop, causing stack overflow.
Example: Exam information and registration information are associated with 1:n
package com.apply.entity; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerator; import com.fasterxml.jackson.annotation.ObjectIdGenerators; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import javax.persistence.*; import java.util.Date; /** * @author tianp * Registration information apply_info */ @Data @Entity @Table(name = "apply_info") @ApiModel(value = "ApplyInfo", description = "registration information object" ) @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") public class ApplyInfo { @Id @GeneratedValue @ApiModelProperty(value = "id", dataType = "long") private Long id; @ManyToOne @JoinColumn(name = "examNum") private ExamInfo examInfo; }
package com.apply.entity; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.ObjectIdGenerators; import lombok.Data; import javax.persistence.*; import java.util.Date; import java.util.Set; /** * @author tianp * Exam information settings apply_examinfo */ @Data @Entity @Table(name = "apply_examinfo") @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") public class ExamInfo { @Id @GeneratedValue private Long id; @OneToMany(mappedBy = "examInfo", cascade = CascadeType.ALL) private Set<ApplyInfo> applyInfos; }
public class Test1 { @Test public void testObject() { ExamInfo examInfo = new ExamInfo(); ApplyInfo applyInfo = new ApplyInfo(); Set<ApplyInfo> set = new HashSet<>(); set.add(applyInfo); examInfo.setApplyInfos(set); applyInfo.setExamInfo(examInfo); System.out.println(examInfo.toString()); System.out.println(applyInfo.toString()); } }
java.lang.StackOverflowError at java.lang.StringBuilder.append(StringBuilder.java:136) at com.apply.entity.ExamInfo.toString(ExamInfo.java:16) at java.lang.String.valueOf(String.java:2994) at java.lang.StringBuilder.append(StringBuilder.java:131) at com.apply.entity.ApplyInfo.toString(ApplyInfo.java:17) at java.lang.String.valueOf(String.java:2994) at java.lang.StringBuilder.append(StringBuilder.java:131) at java.util.AbstractCollection.toString(AbstractCollection.java:462) at java.lang.String.valueOf(String.java:2994) at java.lang.StringBuilder.append(StringBuilder.java:131) at com.apply.entity.ExamInfo.toString(ExamInfo.java:16) at java.lang.String.valueOf(String.java:2994) at java.lang.StringBuilder.append(StringBuilder.java:131) at com.apply.entity.ApplyInfo.toString(ApplyInfo.java:17) at java.lang.String.valueOf(String.java:2994) at java.lang.StringBuilder.append(StringBuilder.java:131) at java.util.AbstractCollection.toString(AbstractCollection.java:462) at java.lang.String.valueOf(String.java:2994) at java.lang.StringBuilder.append(StringBuilder.java:131) at com.apply.entity.ExamInfo.toString(ExamInfo.java:16)
。。。。。。
2.Associating attributes to make the attributes of one party take effect when json is converted
When using SpringMVC's default jackson for json conversion, there are associated properties, and use @JsonIgnore to ignore properties (ignore on the one side,)