Tentei fazer a projeção com a classe Dto, mas, diferente da utilização da interface, utilizando a classe Dto não funcionou, apresentou o seguinte erro:
2022-07-31 19:35:42.364 INFO 13752 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-07-31 19:35:42.370 ERROR 13752 --- [ main] o.s.boot.SpringApplication : Application run failed
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.data.SpringDataApplication.main(SpringDataApplication.java:40) [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.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]
SEGUE O MEU CÓDIGO: public class FuncionarioDto {
private Integer id;
private String nome;
private Double salario;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Double getSalario() {
return salario;
}
public void setSalario(Double salario) {
this.salario = salario;
}
public FuncionarioDto (Integer id, String nome, Double salario) {
this.id = id;
this.nome = nome;
this.salario = salario;
}
}
ESTE É O MÉTODO DA CLASSE FuncionarioRepository: /**
* PROJEÇÃO COM CLASSE DTO
*
*/
@Query(value="SELECT f.id, f.nome, f.salario FROM funcionarios f", nativeQuery = true)
List<FuncionarioDto> findFuncionarioSalarioDto();
ESTE É O MÉTODO DA CLASSE RelatorioService: private void pesquisaFuncionarioSalarioDto() { List list = funcionarioRepository.findFuncionarioSalarioDto(); list.forEach(f -> System.out.println("Funcionario: id: " + f.getId()
+ " | nome: " + f.getNome()
+ " | salario: " + f.getSalario()));
}