• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Rj Ewing
Ranch Hand
Posts: 118
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?



 
Rob Spoor
Sheriff
Posts: 20831
68
Chrome Eclipse IDE Java 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: 118
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.
 
Hug your destiny! And hug this 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!