I came across a project where I have several methods in Repository
of this type:
@Query(value = "SELECT "
+ " i.uf, "
+ " i.cidade "
+ "FROM Imovel AS i "
+ "WHERE i.ativo = 'Sim' AND "
+ " EXISTS (SELECT 1 "
+ " FROM ImovelFoto AS f "
+ " WHERE f.codigoImovel = i.codigo)"
+ "GROUP BY i.uf, i.cidade", nativeQuery = true)
List<Object[]> findUFCidade();
I hope to switch to an object like this:
public class LocalizacaoAgrupadaDTO {
private String uf;
private String cidade;
// Getters e Setters omitidos
}
In this answer in SOen, the solution is to change the query to JPQL.
However, this change adds extra complexity, due to different syntax and entity mappings, and this select is a grouping with no direct relation to the entities.
What is the best way to do this refactoring with Spring Data JPA?
Should I always prioritize the use of JPQL or is it possible with nativeQuery?