创建嵌套实体时发生IllegalStateException

时间:2019-02-02 08:28:46

标签: spring-data h2 java-11

我在实体上有嵌套层次结构,
调查
-问题[]
--Answer []

而且我很难坚持下去。在我添加@PrePersist方法之前,它几乎可以正常工作,子实体对父母具有空的外键。我发现了另一个类似的问题,建议使用@PrePersist来解决该问题,因此我尝试了,但是,这导致了

java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

通过调用此方法返回的

JSON看起来像这样 {"id":1,"title":"Test Survey","description":null,"endingDateTime":null,"questions":[{"id":1,"content":"Test Question","survey":{"id":1,"title":"Test Survey","description":null,"endingDateTime":null,"questions":[{"id":1,"content":"Test Question","survey" ....,并且不断重复。

如果在这种情况下很重要,那么我正在使用Spring Boot 2.1.2和Java11。

@Entity
@Table(name = "survey")
public class Survey implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "survey_id")
    private Long id;

    @OneToMany(cascade = CascadeType.PERSIST, mappedBy = "survey")
    private List<Question> questions = new ArrayList<>();

    @PrePersist
    private void prePersist() {
        questions.forEach(q -> {
            q.setSurvey(this);
        });
    }
}

@Entity
@Table(name = "question")
public class Question implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "question_id")
    private Long id;
    private String content;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "survey_id")
    private Survey survey;

    @OneToMany(cascade = CascadeType.PERSIST, mappedBy = "question")
    private List<Answer> answers = new ArrayList<>();

    @PrePersist
    private void prePersist() {
        answers.forEach(a -> {
            a.setQuestion(this);
        });
    }
}

@Entity
public class Answer implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "answer_id")
    private Long id;
    private String content;
    private Integer votes;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "question_id")
    private Question question;
}

   public Survey createSurvey() {
        Answer answerA = Answer.builder()
                .content("A").build();
        Answer answerB = Answer.builder()
                .content("B").build();
        Question question = Question.builder()
                .content("Test Question")
                .answers(Arrays.asList(answerA, answerB))
                .build();
        Survey survey = Survey.builder()
                .title("Test Survey")
                .questions(Arrays.asList(question))
                .build();
        return surveyRepository.save(survey);
    }

编辑# 如果有帮助,我正在通过GET测试 在stacktrace的结尾,我得到了 Cannot render error page for request [/result] and exception [Could not write JSON: Infinite recursion (StackOverflowError)

不确定为什么会在...

编辑# 登录到H2控制台后,我看到调查,问题和2个答案。每个人都没有孩子与父母的关系。

1 个答案:

答案 0 :(得分:0)

解决方案非常简单,我在所有@OneToMany上添加了@JsonManagedReference

 @OneToMany(cascade = CascadeType.PERSIST, mappedBy = "question")
 @JsonManagedReference
 private List<Answer> answers = new ArrayList<>();

@ManyToOne上我添加了JsonBackReference

 @ManyToOne(cascade = CascadeType.PERSIST)
 @JoinColumn(name = "survey_id")
 @JsonBackReference
 private Survey survey;