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?
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.
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.
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.
A bidirectional relationship is valid in both directions. Intersects is an example of a bidirectional relationship.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With