0
I came across a project where I have several methods in Repository
of this kind:
@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 exchange adds extra complexity, due to the different syntax and even the mapping of the entities, and this select is 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?
You want to turn one
List<Object[]>
inList<LocalizacaoAgrupadaDTO>
? Doing "in hand" would be very bad?– igventurelli
That’s right. Today it’s done by hand in a service class.
– Murillo Goulart