>  기사  >  Java  >  재귀 쿼리를 방지하는 두 가지 방법 소개

재귀 쿼리를 방지하는 두 가지 방법 소개

零下一度
零下一度원래의
2017-06-17 11:58:242001검색

이 글에서는 Spring Boot에서 recursionquery을 방지하는 두 가지 방법을 주로 소개합니다. 두 가지 방법 모두 application.properties에서 구성하고 항목에 주석을 추가하는 방법입니다. 아래를 살펴보세요.

이 기사에서는 재귀 쿼리를 방지하는 Spring Boot 관련 내용을 주로 소개합니다. 여기에는 매우 간단한 두 가지 방법이 있습니다.

1 . 에서 application.propertiesapplication.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<>();
     ......
    }
구성 2. 엔터티에 주석 추가

위 내용은 재귀 쿼리를 방지하는 두 가지 방법 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.