I have two tables in the database, one COUNTRIES and the other CITIES, they have a relationship of ONE to MANY, a country has many cities. I require a query that obtains all the countries with their cities, but if the country does not have associated cities, it should also be in the response of the query. I am doing the following query
select distinct p FROM Pais p LEFT JOIN p.ciudades c
This query meets the objective, however when I look at the log I find that n + 1
queries are made according to the number of existing countries. To avoid multiple queries I do afetch join
select distinct p FROM Pais p JOIN FETCH p.ciudades
With this sentence, when looking at the log, you only see one query made, this reduces n
the number of queries made with respect to the previous sentence, but it does not bring me the countries that do not have associated cities.
How can I bring the countries that do not have associated cities but in such a way that only one query is made and not n +1
queries?