Skip to content
Advertisement

Spring Data JPA Native Query N + 1 problem

I’ve run into some code which behaviour is not clear for me.

We have first entity:

and second one:

And we have next method for retrieving data:

As nativeQuery = true is present I expect this method make just one SQL select and retrieve me data. But If I take a look at logs actually there are 2 selects:

Why this is happening ? How to fix it to make only one select and retrieve all data ?

Advertisement

Answer

It’s not possible to specify native query fetches without Hibernate specific APIs. I would suggest you to use a normal JPQL/HQL query:

This will do the same as your native query but at the same time just run only a single query.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement