2012-11-07 63 views
11

我已經繼承了一個hibernate應用程序,並且遇到了問題。看起來,代碼不會將該子項保存爲一對多關係。它是雙向的,但是在保存父對象時,似乎並沒有保存孩子。hibernate如何保存一對多/多對一的註釋? (兒童不存儲)

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是子類。

// 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; 
    } 
} 

與道:

// QuestionDao.java 
public class QuestionDao { 
    public void saveOrUpdateAll(List<Question> list){ 
     getHibernateTemplate().saveOrUpdateAll(list); 
    } 
} 

看來當我打電話saveOrUpdateAll,問題是保住了,但有問題的對象的兒童AnswerValue都沒有。

有什麼建議嗎?

回答

4

對於oneToMany關係,請定義cascade映射註解爲:

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; 
    } 
2

如果你有問題之間的一個一對多的關係,並回答實體,那麼你爲什麼要使用:

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

我認爲你應該使用這樣的:

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