我正在尝试映射 3 个实体:问题、答案和 QuestionDisplayRule。Question 有很多 Answers 和很多 QuestionDisplayRules,每一个都属于一个 Question。QuestionDisplayRule 有一个问题、一个答案和一个带有字符串值的字段。QuestionDisplayRule 的 db 表如下所示:
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;
}
问题是如果我在数据库中手动添加记录但不会保存它们,它会很好地加载 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);
有任何想法吗?谢谢!