休眠如何保存一对多/多对一注释?(儿童不储蓄)

我继承了一个休眠应用程序,但我遇到了问题。代码似乎没有在一对多关系中保存孩子。它是双向的,但在保存父对象时,它似乎不会保存子对象。

在这种情况下,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.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 已保存,但作为 Question 对象子级的 AnswerValue 却未保存。

有什么建议吗?


答案 1

您需要手动执行此操作或将级联属性添加到注释中,即休眠:级联类型

休眠:如何在注释中使用级联?


答案 2

对于关系,请将映射注释定义为:oneToManycascade

    Cascade={CascadeType.ALL}

     Cascade={ CascadeType.ALL, CascadeType.DELETE_ORPHAN }

在映射中:

    @ManyToOne(fetch = FetchType.EAGER, 
               cascade = { CascadeType.ALL, CascadeType.DELETE_ORPHAN })
    @JoinColumn(name = "question_id",referencedColumnName="question_id")
    public Set<AnswerValue> getAnswerValues(){
        return answerValues;
    }

推荐