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

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

问题描述

我正在阅读关于和我来了一些困难找出一些事情。它必须在 ManyToOne OneToMany 关联之间区别。虽然我在实际项目中使用过它们,但我无法完全理解它们之间的差异。据我的理解,如果一个表/实体与另一个关联 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?

PS:我认为由于它与问题的相关性会有所帮助,如果有人可以解释协会所有者的意义以及双向和单向关联。

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。您可以选择订单和订单行之间具有单向的OneToMany(订单将具有OrderLines的集合)。或者你可以选择在OrderLine和Order之间有一个ManyToOne关联(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.

您选择的解决方案主要取决于情况和级别实体之间的耦合。例如,如果一个用户,一个公司,一个提供商都有很多地址,那么在他们每个人和地址之间都有一个单向的方向是有道理的,而且地址不知道他们的所有者。

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.

假设您有一个用户和一条消息,其中用户可以有数千条消息,但将它建模为仅从消息到用户的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;

在OrderLine中:

in OrderLine :

@ManyToOne
private Order parentOrder;

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

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