Yes, you have understood exactly. Your code would solve the problem. The reason I am looking to avoid this is that for each line-item on my order, I have three associated objects that need to be set. On a 10,000 line order (not uncommon), this would mean that I would perform 30,000 "findById" calls. Even with LazyLoading, this is still a very large number of database lookups that I don't really need, since I already have a valid key for each of these associated objects.
Cameron McKenzie wrote:
So, the item already exists in the database, but you are using the following code:
LineItem.setItem(new Item(itemID);
Why not:
LineItem.setItem(itemDAO.findById(itemID));
Would that solve the problem?
With LazyLoading, or even just swapping load and get methods on the finder implementation, you can avoid actually loading the associated objects.