This article mainly introduces you to two ways to prevent recursionquery in Spring Boot. The two ways are to configure in application.properties and in entity. Add annotations and provide detailed sample code. Friends who need it can take a look below.
This article mainly introduces to you the relevant content about Spring Boot preventing recursive queries. This is just a small reminder. There are two ways here, which are very simple. Let’s take a look at the detailed introduction:
1. Configure
#懒加载配置 spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
2. Add annotations to the entity
Add @JsonBackReference on the associated object
Add @JsonIgnoreProperties("roles")
on the class, fill in the brackets Object to be found
@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<>(); ...... }
The above is the detailed content of Introduce two ways to prevent recursive queries. For more information, please follow other related articles on the PHP Chinese website!