什么时候适合使用双向关联,什么时候不适合?
假设我们想要构建一个类似 eBay 的应用程序,其中有一个名为 Customer
的实体和一个名为 Order
的实体。
从关系角度来看,我将其建模为:
Customer
+----+-----+
| ID | ... |
+----+-----+
Order
+----+-------------+-----+
| ID | CUSTOMER_ID | ... |
+----+-------------+-----+
现在,当我想将其映射到 JPA 时,我有多种选择:
创建从订单到客户的单向多对一关联。恕我直言,这是最接近关系模型的。缺点是,为了查找给定客户的所有订单,我必须编写 JPQL 查询,因为客户对其订单一无所知。另外,我无法以面向对象的自然方式为客户添加新订单(例如
customer.addOrder(aNewOrder);
)。创建从客户到订单的一对多关联。这样,为了查找给定客户的所有订单,我可以使用
customer.getOders()
,并且可以以 OO 自然的方式为客户添加新订单。缺点是,为了找出已下给定订单的客户,我应该使用 JPQL。创建从客户到订单的双向一对多关联。这样,我不需要编写任何 JPQL 查询来检索客户或已下给定订单的客户的所有订单。缺点是增加了维护双向关联的复杂性。
因此,我没有看到关于是否必须使用单向关联或双向关联是否“正确”的明确争论。换句话说,在我看来,这一切都归结为模型设计者/实现者的个人喜好和品味。
我是对的还是有规则可以确定给定关联的正确方向性?
Let's pretend that we want to build an eBay like app in which we have an entity named Customer
and an entity named Order
.
From a relational perspective, I would model this as:
Customer
+----+-----+
| ID | ... |
+----+-----+
Order
+----+-------------+-----+
| ID | CUSTOMER_ID | ... |
+----+-------------+-----+
Now when I want to map this to JPA, I have multiple choices:
Create a uni-directional many-to-one association from Order to Customer. IMHO, this is the closest to the relational model. The downside is that in order to find all orders for a given customer I have to write a JPQL query as a customer does not know anything about its orders. Also I cannot in an OO natural way add a new order for a customer (e.g.
customer.addOrder(aNewOrder);
).Create a one-to-many association from the Customer to Order. This way in order to find all the orders for a given customer I can use
customer.getOders()
and I can add new orders for a customer in an OO natural way. The downside is that in order to find out the customer that has placed a given order I should use JPQL.Create a bidirectional one-to-many association from Customer to Order. This way I don't need to write any JPQL queries to retrieve all orders of a customer or the customer that has placed a given order. The downside is that is added complexity to maintain the bidirectional association.
As such I don't see a definite argument as to whether a unidirectional association has to be used or a bidirectional association is "correct". In other words it seems to me it all boils down to personal preference and taste of the designer/implementor of the model.
Am I right or are there rules according to which it is possible to determine the correct directionality for a given association?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很大程度上取决于每个客户的预期订单数量。如果您预计每个客户有很多订单(想想 eBay),那么让客户拥有与其关联的所有订单将不会非常高效(或者需要花费一些精力来维护惰性关联)。在这种情况下,我推荐方法#1。写一些查询没有什么问题。
但是,如果您预计大多数客户的订单很少,那么双向方法#3 就可以了。
我认为#2 没有多大价值,因为订单始终与一位客户相关联。
Much depends on your expected number of orders per customer. If you expect many orders per customer (think eBay) then having customer with all orders associated with it will not be very efficient (or will take some effort to maintain as lazy association). In this case I recommend approach #1. There is nothing wrong with writing some queries.
However if you expect mostly customers with few orders, then bi-directional approach #3 will work just fine.
I don't see much value in #2 as order is always associated with one customer.