spring-data-jpa——如果使用了one-to-many,many-to-one的注解,在Jackson进行json字符串化时出现错误的解决方案

参考资料:

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

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

在spring-data-jpa中,使用了one-to-many和many-to-one注解,在进行json字符串化时出现错误。

经查阅资料找到以下解决方法:

通过在主表的pojo中增加@JsonManagedReference来注解关联字段:

@OneToMany(cascade = CascadeType.REFRESH, mappedBy="ruleType",targetEntity = Rule.class)
@JsonManagedReference
private Set rule;

在子表的pojo中增加@JsonBackReference来注解关联字段

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

然后通过Jackson来生成json

new ObjectMapper().writeValueAsString(obj);
原文地址:https://www.cnblogs.com/shea/p/7890902.html