• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring repository and n+1 queries on fetched entity.

 
Bartender
Posts: 1359
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using Spring repository to read entities with quite complex relationship among them. More precisely, my repository is querying entities of type A via a fetchByAttribute method.
Now entity A, is in a @ManyToOne relationship with an Entity B, which in turn has a relationship with an Entity C and so on.
The id of each entity is trivially a Long ID.
When I execute a fetchByAttribute method on my repository, I see that with a first query it returns a list of A; then, it performs for each element of the list a query for fetching B entities.
This time, the query performed  actually uses join to fetch the whole "entity chain" (i.e selects A join B join C and so on).
I would have guessed that fetchByAttribute would actually perform a whole join to read all needed data, I cannot explain why I'm running in the n+1 query problem.
By the way, if I annotate the fetchByAttribute method  by a @Query annotation suggesting the whole query (" select a from A a join B b join C c...")  using joins, it looks like that the issue doesn't occur anymore.
Any hint ? I know that without much details it's an hard question to answer, but any help will be really appreciated.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic