@ManyToMany/@OneToMany 双向关联的mappedby属性 [英] @ManyToMany/@OneToMany mappedby attribute for bidirectional association

查看:66
本文介绍了@ManyToMany/@OneToMany 双向关联的mappedby属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的实习开发 JPA 合规工具包……该工具包的一部分涉及测试极端情况的正确实施.

I'm working on a JPA Compliancy kit for my internship... Part of that kit involves testing correct implementation of corner cases.

@ManyToMany 有一个 mappingBy 属性.JPA 声明:

@ManyToMany has a mappedBy attribute. JPA states that:

String mappingBy - 拥有关系的字段或属性.除非关系是单向的,否则是必需的.

String mappedBy - The field or property that owns the relationship. Required unless the relationship is unidirectional.

没有给出默认值 - 默认列是空的.

No default is given - the default column is empty.

给定一个双向 @ManyToMany - 这个例子来自 JPA 2.0 JSR-317 规范本身!

Given a bidirectional @ManyToMany - this example is from the JPA 2.0 JSR-317 specification itself!

客户

@ManyToMany
@JoinTable(name="CUST_PHONES")
public Set<PhoneNumber> getPhones() { return phones; }

电话号码

@ManyToMany(mappedBy="phones")
public Set<Customer> getCustomers() { return customers; }

Customer@ManyToMany 中没有定义mappedBy 属性!是否存在我不知道的双向映射的默认值?

The mappedBy attribute hasn't been defined in the @ManyToMany of Customer! Is there a default for bidirectional mappings that I'm not aware of, or what?

我查看了类似的案例,发现:@OneToOne -mappedBy 是可选的,没有默认值@OneToMany - 与 @ManyToMany 完全相同(mappedBy 是双向可选的,没有默认值)

I looked at similar cases and found: @OneToOne - mappedBy is optional, no default @OneToMany - exactly the same as @ManyToMany (mappedBy is optional for bidirectional, with no default)

简而言之,我的问题是:对于@ManyToMany@OneToMany,关系的拥有方(例如Customer)的mappedBy 属性中应该放置什么?

In short, my question: For @ManyToMany and @OneToMany, what should be placed in the mappedBy attribute for the owning side of the relationship (Customer in example)?

推荐答案

这远非极端情况.每个双向关联都有一个所有者端和一个反向端.

This is far from being a corner case. Every bidirectional association has an owner side and an inverse side.

JPA 使用拥有方来决定两个实体之间是否存在关联.另一边被忽略.

JPA uses the owning side to decide if an association exists between two entities. The other side is ignored.

拥有方是定义关联如何映射的一方(使用 JoinColumn、JoinTable 等注释).它没有任何 mappedBy 属性.

The owning side is the one which defines how the association is mapped (using the JoinColumn, JoinTable, etc. annotations). It doesn't have any mappedBy attribute.

反面使用mappedBy属性表示:嘿嘿,我只是下面属性映射的那个的逆关联".

The inverse side uses the mappedBy attribute to say: "Hey, I'm just the inverse association of the one mapped by the following property".

因此,根据定义,拥有方没有 mappedBy 属性.如果有,就不是拥有方.

So, by definition, the owning side doesn't have a mappedBy attribute. If it had one, it wouldn't be the owning side.

这在 JPA 规范 中有很好的解释.如果您需要为此规范构建合规性工具包,您最好阅读并理解它.

This is well explained in the JPA spec. If you need to build a compliancy kit for this specification, you'd better read and understand it.

我真的不明白编写这样一个合规性工具包的意义,因为正如 JPA2 JSR 主页,

I don't really see the point of writing such a compliancy kit since, as written on the JPA2 JSR home page,

根据 Java 规范参与协议 (JSPA) 的要求,Java Persistence API 2.0 版 TCK 将在任何时候获得许可在不支持符合条件的非营利实体的情况下收费.这样的资格将通过兼容性测试验证奖学金计划.也可以免费提供支持奖学金委员会的批准.欲了解更多信息,请参阅到:http://java.sun.com/scholarship/.

As required by the Java Specification Participation Agreement (JSPA), the Java Persistence API, version 2.0 TCK will be licensed at no charge without support to qualified not-for-profit entities. Such qualification will be verified by the Compatibility Testing Scholarship Program. Support may also be provided at no charge with approval of the scholarship board. For more information, please refer to: http://java.sun.com/scholarship/.

这篇关于@ManyToMany/@OneToMany 双向关联的mappedby属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆