How does hibernate save one-to-many / many-to-one annotations? (Children not saving)

user1732480 picture user1732480 · Nov 7, 2012 · Viewed 49.9k times · Source

I've inherited a hibernate application and I've run into issues. 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.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.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:

// 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.

Any suggestions?

Answer

ilcavero picture ilcavero · Nov 7, 2012

you need to do it manually or add the cascade property to your annotation, i.e. Hibernate: Cascade Type

or Hibernate: How use cascade in annotation?