什么是“拥有方”?在ORM映射? [英] What is the "owning side" in an ORM mapping?

查看:136
本文介绍了什么是“拥有方”?在ORM映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有方 的含义是什么?什么是一些映射示例的解释( 一对多,一对一,多对一 )?

以下文本摘自Java EE 6文档中 @OneToOne 的描述。您可以在其中看到 拥有的一面 这个概念。
$ b


有价值的关联到
另一个具有一对一
多样性的实体。通常不需要
来明确地指定关联的
目标实体,因为它通常可以从所引用的对象的
类型推断
。如果
的关系是双向的,<强>
非所属侧
必须使用OneToOne注释的的mappedBy
元素到
指定持有端的关系字段或
属性。



解决方案

您可以想象拥有方是具有对另一方的引用的实体。在你的节选中,你有一对一的关系。因为它是一个对称关系,所以如果对象A与对象B有关系,那么反之亦然,你最终会得到这个结果。



这意味着保存到对象A中的对象B的引用和保存在对象B中的对象A的引用将是多余的:这就是为什么你要选择哪个对象拥有另一个引用对象的原因。



当你有一对多的关系时,与多部分相关的对象将成为拥有者的一方,否则你将不得不从一个对象中存储许多引用众多。为了避免这种情况,第二个类中的每个对象都会有一个指向它们引用的单个对象的指针(所以它们是所有者的一方)。

对于许多-many关系,因为无论如何你都需要一个单独的映射表,不会有任何拥有方。



总之,拥有方是具有对另一方的引用的实体。

What exactly does the owning side mean? What is an explanation with some mapping examples (one to many, one to one, many to one)?

The following text is an excerpt from the description of @OneToOne in Java EE 6 documentation. You can see the concept owning side in it.

Defines a single-valued association to another entity that has one-to-one multiplicity. It is not normally necessary to specify the associated target entity explicitly since it can usually be inferred from the type of the object being referenced. If the relationship is bidirectional, the non-owning side must use the mappedBy element of the OneToOne annotation to specify the relationship field or property of the owning side.

解决方案

You can imagine that the owning side is the entity that has the reference to the other one. In your excerpt, you have an one-to-one relationship. Since it's a symmetric relation, you'll end up having that if object A is in relation with object B then also the vice-versa is true.

This means that saving into object A a reference to object B and saving in object B a reference to object A will be redundant: that's why you choose which object "owns" the other having the reference to it.

When you have got an one-to-many relationship, the objects related to the "many" part will be the owning side, otherwise you would have to store many references from a single object to a multitude. To avoid that, every object in the second class will have a pointer to the single one they refer to (so they are the owning side).

For a many-to-many relationship, since you will need a separate mapping table anyway there won't be any owning side.

In conclusion the owning side is the entity that has the reference to the other.

这篇关于什么是“拥有方”?在ORM映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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