File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Fetch=join not eager fetching collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Fetch=join not eager fetching collections" Watch "Fetch=join not eager fetching collections" New topic

Fetch=join not eager fetching collections

Baljeet Nijjhar

Joined: Jul 19, 2009
Posts: 2
I was following the examples in the 'Java Persistence and Hibernate' book with Hibernate 3.3.1 to try and load my collection upfront using fetch=join as follows:

I would expect that when I do a session.get(Person.class, personId), that I would automatically fetch any associated Telephones and would not have to do e.g.
However, the collection is not being fetched.

I tried putting lazy="false" in with fetch="join" and all that seemed to happen was an n+1 selects generated (which I want to avoid).

Any ideas?
thanks, Baljeet.
Benjamin Winterberg
Ranch Hand

Joined: Sep 19, 2007
Posts: 36
You could just use FETCH JOINS via HQL:

Hibernate DAOs with Spring | Separation of Concerns | Eclipse Code Templates | more...
Baljeet Nijjhar

Joined: Jul 19, 2009
Posts: 2
The 'problem' is that I did not eagerly fetch the parent collection also using a fetch="join"; I fetched it using Hibernate.initialize(parentCollection).
I thought this would have then loaded it, and my fetch="join" on the child collections would have kicked in, but no.
I don't quite understand why Hibernate.initialize doesn't effectively eager-fetch the parent collection.
I agree. Here's the link:
subject: Fetch=join not eager fetching collections
It's not a secret anymore!