JPA @OneToMany 关系表没有足够的列
在我的应用程序中,我具有以下 @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_ID
和 reservedLessons_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在映射的另一端使用 @ManyToOne 使其成为双向(示例)
You should use @ManyToOne at other side of your mapping to make it bidirectional (example)
我发现了问题。当我命名
ReservedLessons
实体及其reservedLessons
属性时,我很粗心。由于 MySQL 中的 2 个列名实际上是相同的,因此我的连接表缺少应有的列。将ReservedLessons
更改为ReservedLesson
后,我的联接表现在按预期具有 2 列ReservedLesson_ID
和reservedLessons_ID
。I found the problem. I was careless when I was naming my
ReservedLessons
entity and itsreservedLessons
property. Since the 2 column names are actually the same in MySQL, my join table lacks a column it should have. After I change myReservedLessons
toReservedLesson
, my join table now has 2 columnsReservedLesson_ID
andreservedLessons_ID
as expected.