这篇文章主要给大家介绍了关于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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号