JPA @OneToMany 关系表没有足够的列

发布于 2024-12-31 23:13:54 字数 600 浏览 0 评论 0原文

在我的应用程序中,我具有以下 @OneToMany 关系:

@Entity
public class ReservedLessons extends ReservedSessions implements Serializable {

    @OneToMany(cascade={CascadeType.PERSIST})
    private List<Lesson> reservedLessons;

}

@Entity
public class Lesson extends Sessions implements Serializable { ... }

我希望看到包含 2 列 ReservedLessons_IDreservedLessons_ID 的表 reservedlessons_lesson。但是,当我运行该应用程序时,该表只有 1 列 ReservedLessons_ID

在我的应用程序中,我还有其他几个 @OneToMany 关系,并且所有关系都有一个包含 2 列的表。如果您能告诉我我做错了什么,我将非常感激。

In my app, I have the following @OneToMany relationship:

@Entity
public class ReservedLessons extends ReservedSessions implements Serializable {

    @OneToMany(cascade={CascadeType.PERSIST})
    private List<Lesson> reservedLessons;

}

@Entity
public class Lesson extends Sessions implements Serializable { ... }

I expected to see the table reservedlessons_lesson with 2 columns ReservedLessons_ID and reservedLessons_ID. However, when I ran the app, the table only had 1 column ReservedLessons_ID.

In my app, I also have several other @OneToMany relationships and all of them have a table with 2 columns. I'd be very grateful if you could show me what I have done wrong.

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

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

发布评论

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

评论(2

苦行僧 2025-01-07 23:13:54

您应该在映射的另一端使用 @ManyToOne 使其成为双向(示例)

You should use @ManyToOne at other side of your mapping to make it bidirectional (example)

意中人 2025-01-07 23:13:54

我发现了问题。当我命名 ReservedLessons 实体及其 reservedLessons 属性时,我很粗心。由于 MySQL 中的 2 个列名实际上是相同的,因此我的连接表缺少应有的列。将 ReservedLessons 更改为 ReservedLesson 后,我的联接表现在按预期具有 2 列 ReservedLesson_IDreservedLessons_ID

I found the problem. I was careless when I was naming my ReservedLessons entity and its reservedLessons property. Since the 2 column names are actually the same in MySQL, my join table lacks a column it should have. After I change my ReservedLessons to ReservedLesson, my join table now has 2 columns ReservedLesson_ID and reservedLessons_ID as expected.

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