Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
  • 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: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic