[英]Deleting children of OneToMany relationship not working
我从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
我不知道怎么了。 当我将CascadeType.ALL
放在Question getAnswers()
我仍然遇到相同的问题。 我正在使用Spring Data的Hibernate ORM。 另外,当我强制应用程序显示sql命令时,我可以看到Hibernate甚至没有试图删除Answers,只有一个删除-问题。
尝试级联,使用如下
@OneToMany (cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "question",orphanRemoval=true)
此链接您的答案JPA CascadeType.ALL不会删除孤儿
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.