1
resposta

Erro na busca usando projecão e DTO

public class FuncionarioDTO {

  private Integer id;
  private String nome;
  private Double salario;
  public FuncionarioDTO(Integer id, String nome, Double salario) {
    this.id = id;
    this.nome = nome;
    this.salario = salario;
  }
  //query
  @Query(value = "SELECT f.id, f.nome, f.salario FROM funcionarios f order by f.salario desc", nativeQuery = true)
  List<FuncionarioDTO> findFuncionarioDTOSalario();
  //Execução query
  List<FuncionarioDTO> list = funcionarioRepository.findFuncionarioDTOSalario();
    list.forEach(System.out::println);
Erro lançado
java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at br.com.alura.spring.jpa.SpringJpaLocalApplication.main(SpringJpaLocalApplication.java:34) [classes/:na]
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [br.com.alura.spring.jpa.data.orm.FuncionarioDTO]
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.data.repository.query.ResultProcessor$ProjectingConverter.convert(ResultProcessor.java:297) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.lambda$and$0(ResultProcessor.java:217) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:228) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.java:156) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:157) ~[spring-data-jpa-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:142) ~[spring-data-jpa-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at 
(QueryExecutorMethodInterceptor.java:195) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
1 resposta

Segundo a documentação do Spring Data Jpa não é possível usar Projection de DTO com query nativa. https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections.dtos

Insira aqui a descrição dessa imagem para ajudar na acessibilidade

As alternativas mais simples seriam assim: 1- Usar JPQL e passar o construtor do DTO (importante tomar cuidado q tem q passar o caminho completo do pacote até a classe):

    @Query(value = "SELECT new br.com.alura.spring.data.orm.FuncionarioDto(f.id, f.nome, f.salario) FROM Funcionario f")
    List<FuncionarioDto> findFuncionarioSalario();

2 - Usar uma query derivada com base nos atributos da entidade e colocando o DTO como tp de retorno:

List<FuncionarioDto> findAllByOrderByNome();