休眠多对多关系是否必须使用@ManyToMany 注释并使用连接table?

Does a hibernate many to many relationship have to use the @ManyToMany annotation and use a join table?

我在 google 上搜索了很多与 Hibernate 多对多关系相关的示例。我发现所有帖子在这种关系中都使用了join table注解(@JoinTable)。

我必须在多对多中使用@JoinTable 吗?

如果是,为什么?

如果没有,我该如何实现这种关系?

您的问题的答案是肯定的,如果您想为 many-to-many 关联创建架构,使用联接 table 是唯一可用的解决方案.

为什么? - 您需要在两个实体 tables (A & B) 之间加入 table (A.id <--> B.id) 来映射关联。

An example could be student and teachers, a student entity would likely not have a teacher ID FK and a teacher entity would likely not contain a student ID FK

我认为这个 post 会回答 how to implement the relationship.

还要注意双向关联,如果一方是所有者,那么另一方是反向端(即更新关联中的关系值时将被忽略table) : 有mappedBy属性的一面是反面。没有mappedBy属性的一方是所有者。