Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Rj Ewing
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?



 
Rob Spoor
Sheriff
Posts: 20937
81
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: 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!