循环引用就是: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 = "scene

文章介绍了在JavaORM框架中处理循环引用的三种方法:@JsonIgnore、@Transient和@JsonIgnoreProperties。特别推荐使用@JsonIgnoreProperties来避免无限递归,只需在一方实体中忽略对应的属性。例如,在User和EScene类的双向关联中,分别在两个类中设置@JsonIgnoreProperties来忽略对方的引用属性。
最低0.47元/天 解锁文章
224

被折叠的 条评论
为什么被折叠?



