Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it appropriate to use a bidirectional association and when is it not?

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:

  1. 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);).

  2. 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.

  3. 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?

like image 917
Sleepless Caffeine Avatar asked Jun 13 '11 06:06

Sleepless Caffeine


People also ask

Which type of relationship is generally recommended unidirectional or bidirectional?

Prefer bidirectional associations: Unidirectional associations are more difficult to query. In a large application, almost all associations must be navigable in both directions in queries.

What is the difference between unidirectional and bidirectional associativity?

The direction of a relationship can be either bidirectional or unidirectional. A bidirectional relationship has both an owning side and an inverse side. A unidirectional relationship has only an owning side.

What are bidirectional associations?

Bi-directional associations (or symmetric associations ) are the simplest way that two classes can relate to each other. A bi-directional association is shown as a line between two classes, and can contain control points. The classes can be any mix of classes, simple classes, or composite classes.

Which is an example of a bidirectional relationship?

A bidirectional relationship is valid in both directions. Intersects is an example of a bidirectional relationship.


1 Answers

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.

like image 91
Alex Gitelman Avatar answered Oct 17 '22 05:10

Alex Gitelman