1

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);

何か案は?ありがとう!

4

1 に答える 1

1

エンティティを埋め込み可能にマッピングしていますが、これは間違っています。埋め込み可能オブジェクトには、基本型のみを含める必要があります。要するに、エンティティは次のようになります。

@Entity
public class Question
{
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;
    @OneToMany(mappedBy = "question")
    private Set<Answer> answers;
}

@Entity
public class Answer
{
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;

    @ManyToOne
    private Question question;
}

@Entity
public class QuestionDisplayRule
{
    @EmbeddedId
    private QuestionDisplayRulePK key;

    @MapsId(value = "questionId")
    @ManyToOne
    @JoinColumn(name = "question_id", referencedColumnName = "id")
    private Question question;

    @MapsId(value = "answerId")
    @ManyToOne
    @JoinColumn(name = "answer_id", referencedColumnName = "id")
    private Answer answer;
}

@Embeddable
public class QuestionDisplayRulePK
{
    @Column(name = "answer_id")
    private Long answerId;
    @Column(name = "question_id")
    private Long questionId;
}
于 2012-06-07T09:18:34.840 に答える