【问题标题】:IllegalStateException while creating nested entities创建嵌套实体时出现 IllegalStateException
【发布时间】:2019-06-26 17:28:24
【问题描述】:

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

而且我在坚持它时遇到了麻烦。在我添加 @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 和 Java 11。

@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 进行测试 在堆栈跟踪结束时,我得到 Cannot render error page for request [/result] and exception [Could not write JSON: Infinite recursion (StackOverflowError)

不知道为什么会这样……

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

【问题讨论】:

    标签: spring-data h2 java-11


    【解决方案1】:

    解决方案非常简单,在所有@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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多