Home >Java >javaTutorial >Introduce two ways to prevent recursive queries

Introduce two ways to prevent recursive queries

零下一度
零下一度Original
2017-06-17 11:58:242087browse

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

in

application.properties

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn