我想用我的QuestionCompletion
類的所有子元素保持對象。其中一個孩子有一個複合主鍵。作爲這個主鍵的一部分,我還有另一個實體的外鍵。結果我收到此錯誤:使用外鍵級聯保存實體對象作爲複合主鍵的一部分
Exception caught during request processing: javax.ejb.EJBTransactionRolledbackException:
could not set a field value by reflection setter of com.example.model.domain.QuestionCompletionAnswerPK.questionCompletionId
javax.ejb.EJBTransactionRolledbackException: could not set a field value by reflection
setter of com.example.model.domain.QuestionCompletionAnswerPK.questionCompletionId
而最後的「致」當然是NullPointerException
的:
Caused by: java.lang.NullPointerException
這是我的代碼部分。最後一行會導致錯誤。
QuestionCompletion questionCompletion = new QuestionCompletion();
List<QuestionCompletionAnswer> answers = new ArrayList<QuestionCompletionAnswer>();
for (;;) { // loop isn't important; it's loop for answers
ExtendedQuestion extendedQuestion = new ExtendedQuestion();
extendedQuestion.setId(extendedQuestionId); //extendedQuestionId is known to me in that place
for (;;) { // loop isn't important; it's loop for question answers
//questionCompletion and extendedQuestion are popualted here
QuestionCompletionAnswer questionCompletionAnswer = new QuestionCompletionAnswer();
questionCompletionAnswer.setQuestionCompletion(questionCompletion);
questionCompletionAnswer.setExtendedQuestion(extendedQuestion);
answers.add(questionCompletionAnswer);
}
}
questionCompletion.setAnswers(answers);
questionCompletionService.saveOrMerge(questionCompletion);
這是我的基本實體類我想堅持所有的孩子元素。我意識到List<QuestionCompletionAnswer>
會導致問題。我用cascade = CascadeType.ALL
也允許堅持孩子的元素。
@javax.persistence.Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "question_completion")
public class QuestionCompletion implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "question_completion_gen")
@SequenceGenerator(name = "question_completion_gen", sequenceName = "question_completion_id_seq")
@Column(name = "question_completion_id")
private Long id;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "extended_question_id")
protected List<QuestionCompletionAnswer> answers;
}
這是我的課 - QuestionCompletionAnswer
類的主鍵。
@Embeddable
public class QuestionCompletionAnswerPK implements Serializable {
@Column(name = "question_completion_id")
protected Long questionCompletionId;
@Column(name = "extended_question_id")
protected Long extendedQuestionId;
}
這是使用我的EmbeddedId
的類。屬性questionCompletionId
和questionCompletionId
是另一些實體的外鍵,因此我也在下面將這些實體的整個對象與@MapsId
註釋放在一起。
@javax.persistence.Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "extended_question_answer")
public class QuestionCompletionAnswer implements Serializable {
@EmbeddedId
private QuestionCompletionAnswerPK id;
@ManyToOne
@MapsId(value = "questionCompletionId")
@JoinColumn(name = "question_completion_id", nullable = false, insertable = false, updatable = false)
protected QuestionCompletion questionCompletion;
@ManyToOne
@MapsId(value = "extendedQuestionId")
@JoinColumn(name = "extended_question_id", nullable = false, insertable = false, updatable = false)
protected ExtendedQuestion extendedQuestion;
}
您能否告訴我我的註釋是否正確?也許我混合了幾種方法。或者我不能在這種情況下堅持我的基本對象與它的所有子元素。
編輯
現在我的映射是這樣的:
@javax.persistence.Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "question_completion")
public class QuestionCompletion implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "question_completion_gen")
@SequenceGenerator(name = "question_completion_gen", sequenceName = "question_completion_id_seq")
@Column(name = "question_completion_id")
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "questionCompletion")
protected List<QuestionCompletionAnswer> answers;
}
的QuestionCompletionAnswerPK
類的代碼是一樣的。
@javax.persistence.Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "extended_question_answer")
public class QuestionCompletionAnswer implements Serializable {
@EmbeddedId
private QuestionCompletionAnswerPK id;
@ManyToOne(cascade = CascadeType.PERSIST)
@MapsId(value = "questionCompletionId")
@JoinColumn(name = "question_completion_id", nullable = false)
protected QuestionCompletion questionCompletion;
@ManyToOne
@MapsId(value = "extendedQuestionId")
@JoinColumn(name = "extended_question_id", nullable = false)
protected ExtendedQuestion extendedQuestion;
}
有了這個映射,我仍然得到相同的異常。
編輯#2 然而,當我以這種方式改變了QuestionCompletionAnswer
類:
@javax.persistence.Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "extended_question_answer")
public class QuestionCompletionAnswer implements Serializable {
@EmbeddedId
private QuestionCompletionAnswerPK id;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "question_completion_id", nullable = false, insertable = false, updatable = false)
protected QuestionCompletion questionCompletion;
@ManyToOne
@JoinColumn(name = "extended_question_id", nullable = false, insertable = false, updatable = false)
protected ExtendedQuestion extendedQuestion;
}
我得到這個異常:
Caused by: org.hibernate.id.IdentifierGenerationException: null id generated
for:class com.example.model.domain.QuestionCompletionAnswer
不要將@joincolumn添加到onetomany映射。改爲將其標記爲通過擴展問題映射映射。同時刪除manytoone joincolumn def中的updatable = false insertable = false,如mapsid註釋所暗示的那樣。 – Chris 2013-03-06 21:28:17
@Chris感謝您的評論。我已經改變了你所描述的那些線條,但我仍然遇到同樣的錯誤。 – woyaru 2013-03-06 21:58:18
你應該改變上面的代碼,然後顯示你現在有什麼,因爲上面的模型是錯誤的。同時顯示NPE堆棧以查看它來自何處,並確保持續/合併引用的問題,以便分配ID。你可能需要在questionCompletion映射上設置級聯持久化 – Chris 2013-03-07 00:16:34