I'm experimenting with the auction mapping in the hibernate examples. I'd like to load all the auction items and bids into memory with a minimum of queries.
I do one query: load all the AuctionItems:
Session session = factory.openSession();
List auctionItems = session.createQuery("from AuctionItem").list();
Then I iterate over the auctionItems, getting their bids (because it's a lazy collection), generating sql (in hibernate 3)
...
Hibernate: /*load one-to-many org.hibernate.auction.AuctionItem.bids*/select bids0_.item as item__, bids0_.id as id__, bids0_.id as id0_, bids0_.amount as amount1_0_, bids0_.`datetime` as datetime1_0_, bids0_.bidder as bidder1_0_, bids0_.item as item1_0_, bids0_.isBuyNow as isBuyNow0_ from Bid bids0_ where bids0_.item in (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: /*load one-to-many org.hibernate.auction.AuctionItem.bids*/select bids0_.item as item__, bids0_.id as id__, bids0_.id as id0_, bids0_.amount as amount1_0_, bids0_.`datetime` as datetime1_0_, bids0_.bidder as bidder1_0_, bids0_.item as item1_0_, bids0_.isBuyNow as isBuyNow0_ from Bid bids0_ where bids0_.item in (?, ?, ?, ?, ?, ?, ?, ?)
[...]
Where it's preloading 8 bids and trying to be efficient.
But I want to avoid this database hit (say I've got 100k bids). so before I start iterating over AuctionItems, I do the following query:
session.createQuery("from Bid").list();
Which should load all the bids into a cache. But it doesn't.. the queries I was trying to avoid still happen.
I did some reading, and it seems that caches key only by primary key. The item on a bid is a foreign key, and it's nonunique.
So is there any way to be both efficient and idiomatic about this?
Thanks,
Ranjan Bagchi
|