I am reading the Hibernate-In-Action book. In chapter 3, it has a bi-directional many-to-one relationship. Bid and Item.
Code:
public class Bid {
private Item item;
private float amount;
}
public class Item {
private Set bids = new HashSet();
}
My question is if I want query an Item by ID and Bid.amount = 10.0, what is the most efficient to do it?
One thing I think of is this:
Code:
Item item = itemDAO.getItemByID(itemId);
// loop thru in java code to find the amout = 10.0
Set itemBids = item.getBids();
for (Iterator iter = itemBids.iterator(); iter.hasNext();) {
(Bid) element = (Bid) iter.next();
if (element.amount == 10.0) {
// found ....
....
}
}
Is there a better way to do it? I think doing this will require loading all bids of that item to the memory from database. I am trying to come up a better way.
Thank you.