JPA、Hibernate:具有关联的复合键。为什么加载但不保存?

发布于 2025-01-03 21:11:24 字数 1673 浏览 1 评论 0原文

我正在尝试映射 3 个实体:问题、答案和 QuestionDisplayRule。问题有许多答案和许多 QuestionDisplayRules,每个答案都属于一个问题。 QuestionDisplayRule 有一个问题、一个答案和一个具有字符串值的字段。 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;

}

,如果我在数据库中手动添加记录但不会保存它们,它会很好地加载 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);

有什么想法吗?谢谢!

I'm trying to map 3 entities: Question, Answer and QuestionDisplayRule. Question has many Answers and many QuestionDisplayRules each of which belong to one Question. QuestionDisplayRule has one Question, one Answer and one field with String value. The db table for QuestionDisplayRule looks like this:

create table question_display_rule(
    question_id int, 
    answer_id int, 
    answer_value varchar(128)
);

Naturally, question_id and answer_id are the PK.

JPA mapping looks like this:

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

and

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

}

The problem is that it loads QuestionDisplayRules just fine if I add records manually in the DB but won't save them. No errors, no nothing..

Test code:

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

Any ideas? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀞厅☆埖开 2025-01-10 21:11:24

您正在将实体映射到可嵌入对象中,这是错误的。嵌入对象应该只包含基本类型。简而言之,您的实体应该如下所示:

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

You're mapping entities in an embeddable, this is wrong. An embeddable should only contain base types. In short your entities should look like this:

@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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文