I’m trying to figure out the best method of loading more than one association. I’ve been looking through Hibernate in Action, the reference and this forum and everything seems to be focused on one association at a time.
To keep things clear I’ll use the auction system example found in Hibernate in Action. Now, say the Item class had two one-to-many associations, the first association being Bids (as per the book) and the second being Comments.
I know I want to load both of these associations so I try:
Code:
from Item as item
left join fetch item.bids
left join fetch item.comments
This executes fine but when I try and access the comments collection I get “LazyInitializationException: Failed to lazily initialize a collection”. OK, so I expected this because hibernate limits you to fetching one collection eagerly.
The best method I could find in the documentation was lazy loading with the batch-size option (reducing the number of calls). But then I’m required to iterate over the list of returned Items ensuring that the Bids and Comments are initialised.
So, my question is, is there a better/easier way?