• 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

Nested Children Access

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am probably confusing myself on this . . but here it goes.

If I have a fairly deep Parent-> Child Collection -> Child Collection -> Child Collection relationship and I when I get (load)using a DAO (HQL) the parent, what is the best why to retrive specific children from these parent-child collections. Is my only option to iterate thru these collections one by one looking for a specific child (to update for example). Or can I use another HQL statement to retriveById a specfic child, but then how to I assoociate that back to the parent so saving the parent will persist all the children. Thanks for any insight.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, on each and every query you can specific a fetching strategy for each child. With that in mind, there are a few fetching strategies and know what they are and how they work go a long way in determining how performant you application will be in that area.

Using the example of
1 parent has 10 children each of which have 10 childred

Fetching Strategies
Lazy - you have to loop through the children to load them. This will give you the N+1 issue, which means the example will require 1+(10*10) or 101 select statements to the database.

Subselect - will use the first query as a subquery to the next level so in the example will create 1+(1*10) pr 11 total select statements. But each subselect would only run when you touch a children collection.

Eager - Would get all in one query, so to speak. Going deeper in children get a little tricky. But also note that you can only set one child collection to eager so if you had a Parent that has two collections, only one of the collections can be eager loaded. because Hibernate will use a left outer join.

Hope that helps clarify things a bit. I highly recommend getting Java Persistence with Hibernate book which explains things a bit better. Or the online Hibernate documentation.

Good Luck

Mark
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic