Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP 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: 17278
6
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic