Question、Answer、および QuestionDisplayRule の 3 つのエンティティをマップしようとしています。Question には多数の Answer と多数の QuestionDisplayRules があり、それぞれが 1 つの Question に属しています。QuestionDisplayRule には、1 つの質問、1 つの回答、および文字列値を持つ 1 つのフィールドがあります。QuestionDisplayRule のデータベース テーブルは次のようになります。
create table question_display_rule(
question_id int,
answer_id int,
answer_value varchar(128)
);
もちろん、question_id と answer_id は PK です。
JPA マッピングは次のようになります。
@Embeddable
public class QuestionDisplayRulePK implements Serializable {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="question_id", insertable = false, updatable = false)
private Question question;
@ManyToOne
@JoinColumn(name = "answer_id", insertable = false, updatable = false)
private Answer answer;
}
と
@Entity
@Table(name = "question_display_rule")
public class QuestionDisplayRule implements Serializable {
@EmbeddedId
private QuestionDisplayRulePK id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="question_id", insertable = false, updatable = false)
private Question question;
@ManyToOne
@JoinColumn(name = "answer_id", insertable = false, updatable = false)
private Answer answer;
@Column(name="answer_value")
private String answerValue;
}
問題は、DB にレコードを手動で追加しても、保存されない場合、QuestionDisplayRules が問題なくロードされることです。エラーなし、何もありません..
テストコード:
QuestionDisplayRulePK qPK = new QuestionDisplayRulePK();
qPK.setQuestion(q);
qPK.setAnswer(a);
QuestionDisplayRule qr = new QuestionDisplayRule(qPK, "Yes");
qr.setAnswerValue("whateva..");
....
// DAO code looks like: getSession().saveOrUpdate(qr);
何か案は?ありがとう!