如何摆脱无限递归

时间:2019-04-04 19:51:55

标签: spring-boot jackson

我正在尝试解决无穷递归问题,但没有成功! 项目:spring boot和postgresql

我有3个实体:课程,课程大纲和课程表:

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;
...
}

在REST API中,我进行了调用以按课程检索CourseOutline的列表 使用CourseOutline信息库:

List<CourseOutline> findAllByCourse(Course course);

但是我得到以下错误:

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\"])"

0 个答案:

没有答案