Reproduzi o conteúdo das classes do criteria API, e ao tentar isolar a busca pelo nome ele retorna algumas exceptions.
classe RelatorioFuncionarioDinamico
@Service public class RelatorioFuncioarioDinamico {
private final FuncionarioRepository funcionarioRepository;
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
public RelatorioFuncioarioDinamico(FuncionarioRepository funcionarioRepository) {
this.funcionarioRepository = funcionarioRepository;
}
public void inicial(Scanner scanner) {
System.out.println("Digite o nome");
String nome = scanner.next();
if (nome.equalsIgnoreCase("NULL")) {
nome = null;
}
System.out.println("Digite o cpf");
String cpf = scanner.next();
if (cpf.equalsIgnoreCase("NULL")) {
cpf = null;
}
System.out.println("Digite o Salário");
Double salario = scanner.nextDouble();
if (salario == 0) {
salario = null;
}
System.out.println("Digite a data de contratação (dd/MM/yyyy)");
String data = scanner.next();
LocalDate dataContratacao;
if (data.equalsIgnoreCase("NULL")) {
dataContratacao = null;
} else {
dataContratacao = LocalDate.parse(data);
}
List<Funcionario> funcionarios = funcionarioRepository.findAll(Specification
.where(
SpecificationFuncionario.nome(nome))
.or(SpecificationFuncionario.cpf(cpf))
.or(SpecificationFuncionario.salario(salario))
.or(SpecificationFuncionario.dataContratacao(dataContratacao))
);
funcionarios.forEach(System.out::println);
}
}
public class SpecificationFuncionario {
public static Specification<Funcionario> nome(String nome) {
return (root, criteriaQuery, criteriaBuilder) ->
criteriaBuilder.like(root.get("nome"), "%" + nome + "%");
}
public static Specification<Funcionario> cpf(String cpf) {
return (root, criteriaQuery, criteriaBuilder) ->
criteriaBuilder.equal(root.get("cpf"), cpf);
}
public static Specification<Funcionario> salario(Double salario) {
return (root, criteriaQuery, criteriaBuilder) ->
criteriaBuilder.greaterThan(root.get("salario"), salario);
}
public static Specification<Funcionario> dataContratacao(LocalDate dataContratacao) {
return (root, criteriaQuery, criteriaBuilder) ->
criteriaBuilder.greaterThan(root.get("data"), dataContratacao);
}
}
5 - Relatório dinamico
5
Digite o nome
Ander
Digite o cpf
null
Digite o Salário
0
Digite a data de contratação (dd/MM/yyyy)
null
2022-08-13 17:00:32.703 INFO 12028 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-08-13 17:00:32.727 ERROR 12028 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Unable to locate Attribute with the the given name [data] on this ManagedType [br.com.alura.spring.data.orm.Funcionario]; nested exception is java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [data] on this ManagedType [br.com.alura.spring.data.orm.Funcionario]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:374) ~[spring-orm-5.3.22.jar:5.3.22]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:235) ~[spring-orm-5.3.22.jar:5.3.22]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551) ~[spring-orm-5.3.22.jar:5.3.22]
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[spring-tx-5.3.22.jar:5.3.22]
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) ~[spring-tx-5.3.22.jar:5.3.22]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152) ~[spring-tx-5.3.22.jar:5.3.22]
Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [data] on this ManagedType [br.com.alura.spring.data.orm.Funcionario]