0

I am using the Single Table inheritance strategy in Postgres in JPA. Some of my child classes have collection fields of their own, and currently when I do a get on the parent class, it fetches all the rows, then for each row where it needs to fetch the collections it does a separate query.

How do I tell JPA to do only a single joined query with the child collections so I don't face the N+1 query problem? If this was a single class I could have written a JPA query myself and did a join on the child collections, but since I am only doing a get on the parent class, I can't write a query to join on the child collections.

20kLeagues
  • 134
  • 1
  • 6

1 Answers1

0

Beginning from JPA 2.1 to resolve N+1 problem, you can use entity graph and sub-graph where entity graph is a way to describe a query. It has nicely been explained here with coding as well.

In addition, there are also other ways to fix N+1 issue, however, the pros of entity graph is way too much compared to other solutions.

Tejaskumar
  • 754
  • 8
  • 24