【问题标题】:Infinite recursion error with ManyToMany annotation带有 ManyToMany 注释的无限递归错误
【发布时间】:2021-08-04 23:38:02
【问题描述】:

我创建了两个代表数据库中表的类。它们通过带有许多注释的变量连接起来。不幸的是,当我尝试将学生添加到项目中时,出现了无限递归错误。

我尝试通过教程解决这个问题:https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion

我还读到: Spring hibernate @manyToMany relation with @jsonView infinite recursionHibernate many to many with same class, Could not write JSON: Infinite recursion

不幸的是,他们没有提供帮助。

学生网课:

@Entity
@Table(name = "student")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {

    @Id
    @GeneratedValue
    @Column(name="student_id")
    private Integer studentId;

    @Column(nullable = false, length = 50)
    private String imie;

    @Column(nullable = false, length = 100)
    private String nazwisko;

    @Column(unique=true, nullable = false, length = 50, name = "nr_indeksu")
    private String nrIndeksu;

    @Column(nullable = false, length = 50)
    private String email;

    @Column(nullable = false)
    private boolean stacjonarny;

    @ManyToMany(mappedBy = "studenci")
    @JsonIgnore
    private Set<Projekt> projekty;

    @Column(nullable = false, length = 50)
    private String haslo;


}

项目类:

@Entity
@Table(name = "projekt")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Projekt {

    @Id
    @GeneratedValue
    @Column(name = "projekt_id")
    private Integer projektId;

    @Column(nullable = false, length = 50)
    private String nazwa;

    @Column(nullable = false, length = 1000)
    private String opis;

    @Column(nullable = false, name = "dataczas_utworzenia")
    private LocalDateTime dataczasUtworzenia;

    @Column(nullable = false, name = "data_oddania")
    private LocalDate dataOddania;

    @OneToMany(mappedBy = "projekt")
    @JsonIgnoreProperties(value ={"projekt"}, allowSetters = true)
    private List<Zadanie> zadania;

    @ManyToMany
    @JoinTable(name = "projekt_student",
    joinColumns = {@JoinColumn(name="projekt_id")},
    inverseJoinColumns = {@JoinColumn(name="student_id")})
    private Set<Student> studenci;
    
}

错误:

2021-08-04 17:26:15.991  WARN 18144 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[7]->com.project.rest.model.Student["projekty"])]
2021-08-04 17:26:15.991  WARN 18144 --- [nio-8080-exec-3] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@f1ff33b<rs=HikariProxyResultSet@922089472 wrapping org.postgresql.jdbc.PgResultSet@57f69f62>
2021-08-04 17:26:15.991  WARN 18144 --- [nio-8080-exec-3] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@4110461d<rs=HikariProxyResultSet@1176750721 wrapping org.postgresql.jdbc.PgResultSet@57ac7e31>
2021-08-04 17:26:15.991  WARN 18144 --- [nio-8080-exec-3] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@275ad9d4<rs=HikariProxyResultSet@919944575 wrapping org.postgresql.jdbc.PgResultSet@44345fd2>
2021-08-04 17:26:15.991  WARN 18144 --- [nio-8080-exec-3] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@7d7d9e7<rs=HikariProxyResultSet@1405186173 wrapping org.postgresql.jdbc.PgResultSet@33be62da>
2021-08-04 17:26:15.991  WARN 18144 --- [nio-8080-exec-3] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@42db9900<rs=HikariProxyResultSet@1173506052 wrapping org.postgresql.jdbc.PgResultSet@24f79446>
2021-08-04 17:26:15.991  WARN 18144 --- [nio-8080-exec-3] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@1dd679de<rs=HikariProxyResultSet@844941445 wrapping org.postgresql.jdbc.PgResultSet@5d3e700c>
2021-08-04 17:26:15.991  WARN 18144 --- [nio-8080-exec-3] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@43b3e132<rs=HikariProxyResultSet@824582406 wrapping org.postgresql.jdbc.PgResultSet@5bfe18a6>
2021-08-04 17:26:15.991  WARN 18144 --- [nio-8080-exec-3] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@75c8828e<rs=HikariProxyResultSet@1405429510 wrapping org.postgresql.jdbc.PgResultSet@5b58222b>
2021-08-04 17:26:15.991  WARN 18144 --- [nio-8080-exec-3] o.h.e.loading.internal.LoadContexts      : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@73ee203<rs=HikariProxyResultSet@1788253192 wrapping org.postgresql.jdbc.PgResultSet@2caee16a>

【问题讨论】:

    标签: java spring-boot


    【解决方案1】:

    您不应将 @Data 与 JPA 实体一起使用,因为您可能存在双向关系。

    @Data 生成equals/hashCode 和toString。这些生成的方法将导致StackOverflowError

    更好地使用@Getter@Setter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 2019-12-27
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多