我在从数据库中删除OneToMany关系中的孩子时遇到问题。
以下是我的类:
public class Question {
private Set<Answer> answers
/.................
@OneToMany(fetch = FetchType.EAGER, mappedBy = "question", orphanRemoval = true)
public Set<Answer> getAnswers() {
return this.answers;
}
/.................
}和答案类:
public class Answer {
private Question question
/............
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "QUESTION_ID", nullable = false)
public Question getQuestion() {
return this.question;
}
/...........
}我正在尝试删除问题及其所有答案,如下所示:
public void deleteQuestions(List<Question> questions){
for(Question question : questions){
Question databaseQuestion = questionRepository.findOne(question.getId());
for(Answer answer : databaseQuestion.getAnswers()){
answerRepository.delete(answer.getId());
}
questionRepository.delete(databaseQuestion);
}
}我得到的是:
ORA-02292: integrity constraint (DATABASE_NAME.ANSWER_QUESTION_FK) violated - child record found我不知道哪里出了问题。当我在Question getAnswers()上运行CascadeType.ALL时,仍然会遇到同样的问题。我正在对Spring数据使用Hibernate ORM。此外,当我强制app显示sql命令时,我可以看到Hibernate甚至没有尝试删除答案,只有一个删除问题。
发布于 2015-08-11 18:10:52
尝试级联,使用方法如下
@OneToMany (cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "question",orphanRemoval=true)https://stackoverflow.com/questions/31938768
复制相似问题