Hibernate/JPA ManyToOne vs OneToMany [英] Hibernate/JPA ManyToOne vs OneToMany

查看:24
本文介绍了Hibernate/JPA ManyToOne vs OneToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读有关 实体关联,我在弄清楚一些事情时遇到了一些困难.它本质上与ManyToOneOneToMany 关联之间的区别有关.虽然我在实际项目中使用过它们,但我无法完全理解它们之间的区别.据我了解,如果一个表/一个实体与另一个有 ManyToOne 关联,那么该关联应该来自另一侧 OneToMany.那么,我们应该如何根据具体情况决定选择哪一个,以及它如何影响数据库/查询/结果?到处都有很好的例子吗?

I am reading currently the documentation of Hibernate regarding the entity associations and I come accross a little difficulty to figure out some things. It has to do in essence with the difference between ManyToOne and OneToMany associations. Although I have used them in real projects, I cannot apprehend completely the differnce between them. To my understanding, if a table / an entity has a ManyToOne association with another, then the association should be from the other side OneToMany. So, how should we decide which one to choose based on a specific case and how does it affect the database/queries/results? Is there everywhere a good example?

P.S.:我认为由于它与问题的相关性,我认为这会有所帮助,如果有人能解释一下关联所有者的意义是什么以及双向关联和单向关联之间的区别.

P.S.: I reckon it would be helpful due to its relevance to the question, if someone could besides explain what is the point of the owner of the association and the difference between Bidirectional and Unidirectional association.

推荐答案

假设您有一个 Order 和一个 OrderLine.您可以选择在 Order 和 OrderLine 之间使用单向 OneToMany(Order 将具有 OrderLine 的集合).或者,您可以选择在 OrderLine 和 Order 之间建立多对一关联(OrderLine 将引用其 Order).或者,您可以选择同时使用两者,在这种情况下,关联将成为双向 OneToMany/ManyToOne 关联.

Suppose you have an Order and an OrderLine. You can choose to have a unidirectional OneToMany between Order and OrderLine (Order would have a collection of OrderLines). Or you can choose to have a ManyToOne association between OrderLine and Order (OrderLine would have a reference to its Order). Or you can choose to have both, in which case the association becomes a bidirectional OneToMany/ManyToOne association.

您选择的解决方案主要取决于情况,以及实体之间的耦合程度.例如,如果一个用户、一家公司、一个提供商都有许多地址,那么在每个地址和 Address 之间设置一个单向地址是有意义的,并且 Address 不知道他们的所有者.

The solution you choose mainly depends on the situation, and on the level of coupling between the entities. For example, if a user, a company, a provider all have many addresses, it would make sense to have a unidirectional between every of them and Address, and have Address not know about their owner.

假设您有一个 User 和一个 Message,其中一个用户可以有数千条消息,将其建模为从 Message to User 的 ManyToOne 是有意义的,因为您很少会询问用户的所有消息反正.关联可以是双向的,但只是为了帮助查询,因为 JPQL 查询通过浏览实体的关联来连接实体.

Suppose you have a User and a Message, where a user can have thousands of messages, it could make sense to model it only as a ManyToOne from Message to User, because you'll rarely ask for all the messages of a user anyway. The association could be made bidirectional only to help with queries though, since JPQL queries join between entities by navigating through their associations.

在双向关联中,您可能会遇到对象图不一致的情况.例如,订单 A 将有一组空订单行,但某些订单行会引用订单 A.JPA 强制要求始终将关联的一侧作为所有者侧,而另一侧作为相反侧.JPA 忽略反面.所有者方是决定存在什么关系的一方.在 OneToMany 双向关联中,所有者端必须是多端.因此,在前面的示例中,所有者端将是 OrderLine,并且 JPA 将保留行与订单 A 之间的关联,因为这些行引用了 A.

In a bidirectional association, you could be in a situation where the graph of objects is inconsistent. For example, Order A would have an empty set of OrderLines, but some OrderLines would have a reference to the Order A. JPA imposes to always have one side of the association being the owner side, and the other side being the inverse side. The inverse side is ignored by JPA. The owner side is the side that decides what relation exists. In a OneToMany bidirectional association, the owner side must be the many side. So, in the previous example, the owner side would be OrderLine, and JPA would persist the association between the lines and the order A, since the lines have a reference to A.

这样的关联会像这样映射:

Such an association would be mapped like this:

按顺序:

@OneToMany(mappedBy = "parentOrder") // mappedBy indicates that this side is the 
   // inverse side, and that the mapping is defined by the attribute parentOrder 
   // at the other side of the association.
private Set<OrderLine> lines;

在订单行中:

@ManyToOne
private Order parentOrder;

这篇关于Hibernate/JPA ManyToOne vs OneToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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