我继承了一个休眠应用程序,但遇到了问题。似乎代码并没有将孩子保存在一对多的关系中。它是双向的,但是在保存父对象时,它似乎并没有保存子对象。
在这种情况下,问题类是父类。
// Question.java
@Entity
@SequenceGenerator(name = "question_sequence", sequenceName = "seq_question", allocationSize = 1000)
@Table(name = "question")
public class Question {
protected Long questionId;
protected Set<AnswerValue> answerValues;
public TurkQuestion(){}
public TurkQuestion(Long questionId){
this.questionId = questionId;
}
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "question_sequence")
@Column(name = "question_id")
public Long getQuestionId(){
return questionId;
}
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "question_id",referencedColumnName="question_id")
public Set<AnswerValue> getAnswerValues(){
return answerValues;
}
public void setQuestionId(Long questionId){
this.questionId = questionId;
}
public void setAnswerValues(Set<AnswerValue> answerValues){
this.answerValues = answerValues;
}
}
AnswerValue 是子类。
// AnswerValue.java
@Entity
@SequenceGenerator(name = "answer_value_sequence", sequenceName = "seq_answer_value", allocationSize = 1)
@Table(name = "answer_value")
public class AnswerValue {
protected Long answerValueId;
protected Question question;
protected String answerValue;
public AnswerValue(){}
public AnswerValue(Long answerValueId, Long questionId, String answerValue){
this.answerValueId = answerValueId;
this.question = new Question(questionId);
this.answerValue = answerValue;
}
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "answer_value_sequence")
@Column(name = "answer_value_id")
public Long getAnswerValueId(){
return answerValueId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question_id", nullable = false )
public Question getQuestion(){
return question;
}
@Column(name = "answer_value")
public String getAnswerValue(){
return answerValue;
}
public void setAnswerValueId(Long answerValueId){
this.answerValueId = answerValueId;
}
public void setQuestion(Question question){
this.question = question;
}
public void setAnswerValue(){
this.answerValue = answerValue;
}
}
和 DAO:
// QuestionDao.java
public class QuestionDao {
public void saveOrUpdateAll(List<Question> list){
getHibernateTemplate().saveOrUpdateAll(list);
}
}
似乎当我调用 saveOrUpdateAll 时,问题已保存,但作为 Question 对象的子项的 AnswerValue 未保存。
有什么建议么?