Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Fetch=join not eager fetching collections

 
Baljeet Nijjhar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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.
Hibernate.initialise(person.getTelephones).
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could just use FETCH JOINS via HQL:

 
Baljeet Nijjhar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic