Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested Children Access

 
joe priv
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic