Suppose I have to develop a simple data model in Java for Order, which contains Order Items. It looks like Order should hold a reference to a collection Order Items Now what if Order and Order Items are stored in a database? Should the Order still hold a reference to the collection or just a simple function retrieveItemsByOrderId should be provided instead?
Suppose I have to develop a simple data model in Java for Order ,
Share
This would depend on how your object model is used by the persistence layer to map classes to the database tables. If you are using Hibernate/JPA/EclipseLink/Toplink or a similar ORM framework, you would merely have a getter method in your
Orderclass that would return the collection ofOrderIteminstances. Partial code representation would be:I haven’t listed all annotations in use by the frameworks, including the keys for each entity class, but you’ll need to do this to get things working. The salient points however are:
Orderclass contains the Id, which may be the natural key (or may be a generated one).getOrderItemsmethod will result in theSetof order items associated with an order to be returned. Note that most ORMs will lazily fetch collections, so you’ll need to understand a few more concepts like working with managed and detached entities to actually get this to work; you might need to write an application service to do the work of merging detached entities and then fetch the collection.One of the comments stated that there is no need to reference the
Orderfrom theOrderItemclass. This would lead to a unidirectional relationship instead of a bidirectional one. You can use unidirectional relationships in most ORM frameworks, but consider the following:OrderfromOrderItemwithout any further effort on your part, while others might require you to use a Join table. If you are persisting an object graph in the database, then it is imperative to know whichOrderItemmaps to anOrder; by removing the reference from theOrderItem, you will be forced to map this information elsewhere, in a different entity and usually resulting in a different table; this is the Join table that are referred to previously.Orderis responsible for accessingOrderIteminstances, then you do not need bidirectional relationships. But if you find yourself needing to access theOrderfor anOrderItem, then you will need a bidirectional relationship. I would suggest reading the Mutual Registration Pattern, so that you will always be able to maintain referential integrity irrespective of any mutation operations performed onOrderorOrderItemclasses in such a case. Without that pattern, you are almost always going to find yourself seeing vague, unexplained and incorrect object graphs resulting in an inconsistent database state.If you are not using ORM or you don’t intend to, then it would depend on you are accessing the
OrderIteminstances; in short, it depends on how you are writing your persistence layer. If you are using the DAO pattern, then adding a new methodretrieveItemsByOrderIdinto your DAO interface would be the solution.