• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

spring data jpa and hibernate n+1 selects on manytoone column

 
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using spring data jpa, hibernate, and mysql. When I load an entity with a manytoone column, hibernate is issuing multiple select statements. This article seems to explain the issue a bit, and claims that eager fetching should never be used.

We are not having performance issues, so I wasn't planning on optimizing and returning only the data needed in each query, however I would like to fix the n+1 issue.

How can I instruct hibernate/spring jpa to use a join instead of multiple selects?



 
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a fetch join in your select statement. Both JPQL and the criteria query API support fetch joins.
 
Rj Ewing
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that the only way? Is there a setting or something in spring data jpa to use fetch join statements?

One of the advantages of using spring data jpa was that I the query statements are generated from the method names. It seems like a join fetch would be preferred over multiple selects.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic