循环引用就是:A中有B,B中有A。一查询,就无限递归了。
现在给出解决方案(以及我认为,最好的方案。)
1 @JsonIgnore
直接忽略对象,简单粗暴,变成单项关联了。
2 @Transient
简单粗暴,直接从数据库映射了。
3 @JsonIgnoreProperties(value = { "xxx" })
这是jackson的注解。
这个比较推荐
这里是多对多的例子:
User 对象设置@JsonIgnoreProperties(value = { "users" }) //users代表EScene中的users对象
@Entity @Table(name = "sys_user") public class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String username; @JsonIgnoreProperties(value = { "users" }) @ManyToMany(fetch = FetchType.EAGER) @Fetch(FetchMode.SUBSELECT) @JoinTable(name = "e_user_scene", joinColumns = { @JoinColumn(name = "uid") }, inverseJoinColumns = { @JoinColumn(name = "sceneId") }) private List<EScene> scenes; //get,set... }
EScene 对象设置@JsonIgnoreProperties(value = { "senes" }) //senes代表User中的senes对象
@Entity @Table(name = "e_scene",catalog = "evisu") public class EScene implements java.io.Serializable { private static final long serialVersionUID = 427407430798803968L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @JsonProperty("sceneId") private Integer sceneId; @JsonIgnoreProperties(value = { "senes" }) @ManyToMany(fetch = FetchType.EAGER) @Fetch(FetchMode.SUBSELECT) @JoinTable(name = "e_user_scene", joinColumns = { @JoinColumn(name = "sceneId") }, inverseJoinColumns = { @JoinColumn(name = "uid") }) private List<User> users; //get,set }
标签:name,jpa,value,sceneId,private,循环,引用,JsonIgnoreProperties,users From: https://www.cnblogs.com/hanjun0612/p/17210636.html