java - @ManyToMany(mappedBy = "foo")

标签 java jpa orm many-to-many

Foo 有:

@ManyToMany(mappedBy = "foos")
private Set<Bar> bars

而酒吧有:

@ManyToMany
private Set<Foo> foos

除了table是叫foo_bar还是bar_foo之外,mappedBy属性的位置对双向关系有什么不同;如果没有 mappedBy 属性,我会得到两个连接表,foo_bar 和 bar_foo。

最佳答案

The documentation说:

If the association is bidirectional, one side has to be the owner and one side has to be the inverse end (ie. it will be ignored when updating the relationship values in the association table):

因此,具有 mappedBy 属性的一面是反面。没有 mappedBy 属性的一方是所有者。

所有者方是 Hibernate 查看以了解存在哪个关联的一方。因此,例如,如果您在 Bar 的 foo 集合中添加一个 Foo,Hibernate 将在连接表中插入一个新行。相反,如果您将 Bar 添加到 Foo 的 bar 集合中,则不会在数据库中修改任何内容。

关于java - @ManyToMany(mappedBy = "foo"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14111607/

相关文章:

java - 在 Java 正则表达式中捕获组

hibernate - 无法删除或更新父行 : a foreign key constraint fails with Join tables

python - 计算 Django ORM 中按查询分组的注释字段的最大总和?

ruby - 使用 EventMachine 进行异步数据库访问

java - 我可以修改JPA中的id类型,从 "int"到 "long"而不影响数据库中已存在表中的数据

java - ManyToMany 关系以及 ManyToMany 与另一个类的关系的联接表

java - 运行单元测试时对 assertEquals 的引用不明确

java - 当应该返回 true 时 hashset 返回 false

邮件 : How do I tell gmail not to sign me into chat?

java - 如何在不更新optimistic_force_increment hibernate jpa中的实体数据的情况下更改版本?