• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Alternating between Eager and Lazy Fetching

Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code module, It fetches all objectes of Trans

Please how do i specify the type of fetching i want. At certain times i would want it to retrieve all associated objects (Eager), At other times it should not retrieve all associated objects
Posts: 17346
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To eager fetch an association, you put a JOIN FETCH in the From clause. It can be LEFT or INNER join fetches.

so HQL would look like

Select p FROM Parent p LEFT JOIN FETCH p.children

One caveat is that you can only eager fetch one OneToMany or ManyToMany association. This is because if you do more than one you get a cartesian product.

You can do one Left and one Inner join fetch of OneToMany or ManyToMany association, but be sure that for the Many side, there is a guarantee that there is always at least one returned or the Parent doesn't load in the query. Just like an inner join in SQL resultset would do.

You can also look into the Fetching strategy of "subselect" so that the Many side association is loaded in just one more query rather than N number of queries.

Good Luck

Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
criteria queries allow you to specify a fetch mode
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic