Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Alternating between Eager and Lazy Fetching

 
Abiodun Adisa
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac 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

Mark
 
Joe May
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
criteria queries allow you to specify a fetch mode
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!