0 Replies Latest reply on Oct 25, 2008 6:11 PM by rickcr

    With JPA, is there an easy way to force nested lazy collecti

    rickcr

      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 other 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, and typically I suppose I should 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.