介绍防止递归查询的两种方式

零下一度
零下一度 原创
2017-06-17 11:58:24 1512浏览

这篇文章主要给大家介绍了关于Spring Boot中防止递归查询的两种方式,两种方式分别是在application.properties中配置和在entity中添加注解,都给出了详细的示例代码,需要的朋友们下面来一起看看吧。

本文主要给大家介绍了关于Spring Boot防止递归查询的相关内容,这只是一个小提醒,这里有两种方式,很简单,下面来看看详细的介绍:

1、在application.properties中配置


#懒加载配置
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

2、在entity中添加注解

  • 在关联对象上添加@JsonBackReference

  • 在类上添加@JsonIgnoreProperties("roles") ,括号中填写不需要查出的对象


@Entity
@Table(name = "users")
//@JsonIgnoreProperties("roles")
public class User implements Serializable {
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Id
 private int id;
 @Column
 private String name;
 @Column(name = "created_at")
 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 private Date createdAt;
 @ManyToOne
 @JoinColumn(name = "dep_id")
 @JsonBackReference //防止关系对象的递归访问
 private Department department;
 @ManyToMany(cascade = {}, fetch = FetchType.EAGER)
 @JoinTable(name = "user_role", joinColumns = {@JoinColumn(name = "user_id")}, inverseJoinColumns = {@JoinColumn(name = "role_id")})
 @JsonBackReference
 private List<Role> roles = new ArrayList<>();
 ......
}

以上就是介绍防止递归查询的两种方式的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。