[英]How does hibernate save one-to-many / many-to-one annotations? (Children not saving)
I've inherited a hibernate application and I've run into issues. 我继承了一个hibernate应用程序,但我遇到了问题。 It seems that the code does not save the child in a One-To-Many relationship. 似乎代码不会将孩子保存为一对多关系。 It's bidirectional, but on save of parent object, it doesn't seem to save the child. 它是双向的,但是在保存父对象时,它似乎不会保存孩子。
The Question class is the parent in this case. 在这种情况下,Question类是父类。
// 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 is the child class. 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;
}
}
And the DAO: 和DAO:
// QuestionDao.java
public class QuestionDao {
public void saveOrUpdateAll(List<Question> list){
getHibernateTemplate().saveOrUpdateAll(list);
}
}
It seems when I call saveOrUpdateAll, Question is saved, but the AnswerValue that are children of the Question object are not. 当我调用saveOrUpdateAll时,似乎保存了Question,但是Question对象的子元素AnswerValue却没有。
Any suggestions? 有什么建议?
you need to do it manually or add the cascade property to your annotation, ie Hibernate: Cascade Type 你需要手动完成它或者将cascade属性添加到你的注释中,即Hibernate:Cascade Type
or Hibernate: How use cascade in annotation? 或者Hibernate:如何在注释中使用级联?
For oneToMany
relationship, please define the cascade
mapping annotation as: 对于oneToMany
关系,请将cascade
映射注释定义为:
Cascade={CascadeType.ALL}
or 要么
Cascade={ CascadeType.ALL, CascadeType.DELETE_ORPHAN }
In your mapping: 在您的映射中:
@ManyToOne(fetch = FetchType.EAGER,
cascade = { CascadeType.ALL, CascadeType.DELETE_ORPHAN })
@JoinColumn(name = "question_id",referencedColumnName="question_id")
public Set<AnswerValue> getAnswerValues(){
return answerValues;
}
If you have a one-to-many relationship between the Question and Answer entities, then why are you using: 如果问题和答案实体之间存在一对多的关系,那么您为什么使用:
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "question_id",referencedColumnName="question_id")
public Set<AnswerValue> getAnswerValues(){
return answerValues;
}
I think you should use this instead: 我认为你应该使用它:
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "question_id",referencedColumnName="question_id")
public Set<AnswerValue> getAnswerValues(){
return answerValues;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.