• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

With JPA how can I force loading of nested lazy collections?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(I also posted this on a jboss jpa forum and haven't received a response yet, so trying here as well...)

I'm new to JPA (and Hibernate for that matter. Come from iBATIS background.)

Take a case where you have a Company object which contains a List of Employees, and each Employee object has List of Address objects and List of Preferences (Address and Preference would be another domain models.)

I obviously don't want to to return some of the nested items all of the time, so lazy seems to be fine for the nested collections, and typically I suppose I should use 'join fetch' to pull back some of the nested items. However, in the Employee object there are two Collections and I've read it's not a good idea (if even possible?) to join fetch two different collections within the same object.

My question is how can I force the lazy loading of some objects so that they are all populated without traversing the entire nested structure and having to call the getter on the lazy fields just to get them populated?

It looks like in Hibernate you can call Hibernate.initialize(yourObject.getYourCollection) and it will initialize the lazy load of the objects, which is bit cleaner than yourObject.getYourCollection.size(). But not sure how to accomplish the same thing with just standard JPA syntax?

Also how does this work if you are nested more than one level and I need my nested lazy collections also populated? (For example maybe I need the whole object tree populated before sending it to the web tier or off to some other business process outside of the scope of my EntityManager.)

Maybe I can manually set the fetch type to Eager for those domain models before performing my specific query? I haven't seen an example of that done though.

Thanks for any help.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UP,

Experiencing the very same problem
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your case, you need to declare "outer-join=true" on the collections and also set "hibernate.max_fetch_depth" to retrieve the collections.

If collections are declared lazy, we can use the below command to initialize the collections before closing session:



 
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Upto what extent does this initialize collection ?
What if the object in the collection contains another collection... ?
 
Daniel Melo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember we're using JPA, so there is no Hibernate.initialize.

Let's say, I've the following class design:

Every collection is lazy loaded by default.

I'd like to get all Roles of a particular User. The point is, each single Role returned must have it's Permissions eagerly loaded as well. The JPQL query:
must be changed to accomplish this.
I've tried different combinations with join fetch, with no success.

Any help?

thanks!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found a solution for what you asked, I know that it will not help you to get answer after around 3 years
But i liked to reply to help other poeple searching for the same solution

You can join fetch option like this example.

createQuery("select c from Category as c" + " left join fetch c.categorizedItems as ci" + " join fetch ci.item as i");



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic