I have a class called Order and a class called LineItem.
Now this is fine if I am retrieving from the db the entire order with all the lineItems. But most of the time I am retrieving just one lineItem. I still need the order info too. I have thought of doing this:
This seems sloppy using one member currentLineItem and a List lineItems together. I have also thought of doing this to retrieve the current lineItem from order:
LineItem lineItem = order.getLineItems().get(0);
This seems sloppy too, since I can never really be sure that the List will only have one LineItem in it without checking and it is the one I retrieved. I like the first example better, but it still doesn't seem right. What would be the best way of representing this Order class given that most of the time I am only needing to retrieve one Order with one lineItem?
Please use the code button; I have edited this post so you can see how much better it is to read. CR[/edit]
posted 9 years ago
I have got this post in the wrong forum. It should be in Intermediate.
You simply need to check your list of line items first:
Is it empty? If it's not empty then grab the 1st line item as the current line item (Note, you're assuming that the first item in the list is the current line item, but are you guaranteeing this definition/order?) as you stated above ...get.(0)..
Have you considered adding an Order property to the LineItem?
This would be the inverse relationship to the List of LineItems that the Order has. The idea being that if you were to get a handle to an individual LineItem, you would then have the reference back to its Order.
Are you doing some sort of Object Relational mapping to load these from the database?