I haven’t done much Java programming and hence a lot of unanswered ORM questions revolve in my head that might seem as fairly straight forward to more seasoned folks.
Let's say we have two classes: Customer and Order. Customer class implements a method called listAllOrders, what should the method’s signature be?
Set<Order> getAllOrders();
// the OOP waySet<Integer> getAllOrders();
// db-friendly way, based on the assumption that each order is assigned a unique int idint[] getAllOrders();
// db-friendly way, optimisedSet<OrderID> getAllOrders();
// the OOP way, optimisedOther? A combination of the above through method overloading?
My thinking is to dismiss 3rd option right away, since the optimisation is premature, uncalled for and is likely to cause more trouble than good.
Choosing between the 1st and the 2nd options, the main argument seems to be that in many scenarios returning a collection of orders instead of id’s is going to be an overkill.
The 1st option would always require pre-loading of orders into memory and although some order properties can be handled through lazy loading the Order objects themselves would still require significantly more memory allocated as opposed to a set of bare id’s. The other reason seems to be that lazy loading won’t give me the advantage of using the final
modifier to enforce immutability of Order fields.
However, option 2 doesn’t really encapsulate order numbering, i.e. if it were decided to start using String UUID or long as order identifier instead of integer it would become necessary to do a serious rewrite. Obviously this could be mitigated by introduction of a new lightweight object called OrderId (4th approach).
Well, at this point I'd really appreciate some help from ORM and Java gurus!