0

i'm trying to solve infinity recursion issue with no success ! project: spring boot and postgresql

i have 3 entities : course, course outline and course schedule :

public class Course implements Serializable {

    private static final long serialVersionUID = -6645577819394287204L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID id;


    @OneToMany(mappedBy = "course", cascade = CascadeType.REMOVE)
    @OrderBy("rank ASC")
    private List<CourseOutline> outlines;
....
}


public class CourseOutline implements Serializable {

    private static final long serialVersionUID = -6645577819394287204L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID id;

    @ManyToOne
    @JoinColumn(name = "course", nullable = false)
    @JsonIgnore
    private Course course;

    private Integer rank;



    @OneToMany(mappedBy = "outline", cascade = CascadeType.REMOVE)
    @OrderBy("day DESC, started ASC")
    private Set<CourseSchedule> schedules;
...
}


public class CourseSchedule implements Serializable {

    private static final long serialVersionUID = -6645577819394287204L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID id;

    @ManyToOne
    @JoinColumn(name = "instructor", nullable = true)
    @JsonManagedReference
    private Person instructor;

    @ManyToOne
    @JoinColumn(name = "outline", nullable = false)
    @JsonIgnore
    private CourseOutline outline;
...
}

in REST API i make a call to retrieve a List of CourseOutline by course using CourseOutline Repository :

List<CourseOutline> findAllByCourse(Course course);

but i get below error :

Could not write JSON: Infinite recursion (StackOverflowError); nested
exception is com.fasterxml.jackson.databind.JsonMappingException:
Infinite recursion (StackOverflowError) (through reference chain:
java.util.ArrayList[2]-myDomain.api.models.entities.CourseOutline[\"schedules\"])"
alveomaster
  • 1,681
  • 2
  • 12
  • 21
  • 1
    Do you use [jackson-datatype-hibernate](https://github.com/FasterXML/jackson-datatype-hibernate) module? `@JsonIgnore` annotation should work and break cycle. Take a look on [Jackson – Bidirectional Relationships](https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion) for other options. Take a look on [Jackson/Hibernate, meta get methods and serialization](https://stackoverflow.com/questions/55383889/jackson-hibernate-meta-get-methods-and-serialization). If nothing will work, try to remove all `Hibernate` annotations and write serialisation test. – Michał Ziober Apr 04 '19 at 22:48
  • Put @JsonIgnore on the getters not on fields. – Strelok Apr 05 '19 at 02:31
  • thx @MichałZiober, that solves my problem, with use of `implementation("com.fasterxml.jackson.core:jackson-annotations:2.9.8") implementation("com.fasterxml.jackson.datatype:jackson-datatype-hibernate5:2.9.8")` and `@Bean public Module hibernateModule() { return new Hibernate5Module();}`, the only thing that i have to underline here is that i'm constraint to replace @Data lombok annotation explicitly by @Setter and @Getter and omit @EqualsAndHashCode annotation witch causes a nested error at serialization phase `@Data @EqualsAndHashCode(callSuper = false) public class CourseSchedule...` – alveomaster Apr 05 '19 at 10:43
  • `Hibernate` creates proxies and we need to take care about this. Using `Lombok` with `Jackson` creates many subtle problems and we need to take care about them as well. I always recommend to generate all `setters`/`getters` in `IDE` and do not mix `Lombok` and `Jackson` and `Hibernate` – Michał Ziober Apr 05 '19 at 10:49

0 Answers0