I created two classes that represent tables in the database. They are connected through variables with manytmany annotations. Unfortunately, when I am trying to add a student to the project, an infinite recursion error appears.
I tried to solve this with the help of a tutorial: https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
I also read: Spring hibernate @manyToMany relation with @jsonView infinite recursion and Hibernate many to many with same class, Could not write JSON: Infinite recursion
In my case they unfornately did not help.
Studnet class:
@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;
}
Projekt class:
@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;
}
Error:
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>