Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

[Dúvida] Erro de no bean validation

Bom dia a todos,

Estou tentando executar a aplicação após criar as validações de regra de negocio e agendamento de consulta. Segue o log de erro abaixo:

Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'consultaController': Unsatisfied dependency expressed through field 'agenda': Error creating bean with name 'agendaDeConsultas': Unsatisfied dependency expressed through field 'consultaRepository': Error creating bean with name 'consultaRepository' defined in med.voll.api.domain.consulta.ConsultaRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Could not create query for public abstract boolean med.voll.api.domain.consulta.ConsultaRepository.existsByMedicoIdData(java.lang.Long,java.time.LocalDateTime); Reason: Failed to create query for method public abstract boolean med.voll.api.domain.consulta.ConsultaRepository.existsByMedicoIdData(java.lang.Long,java.time.LocalDateTime); No property 'data' found for type 'Long' Traversed path: Consulta.medico.id. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:716) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:696) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:483) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.11.jar:6.0.11] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:942) ~[spring-context-6.0.11.jar:6.0.11] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.11.jar:6.0.11] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.0.10.jar:3.0.10] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-3.0.10.jar:3.0.10] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-3.0.10.jar:3.0.10] at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) ~[spring-boot-3.0.10.jar:3.0.10] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[spring-boot-3.0.10.jar:3.0.10] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[spring-boot-3.0.10.jar:3.0.10] at med.voll.api.ApiApplication.main(ApiApplication.java:10) ~[classes/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.0.10.jar:3.0.10]

2 respostas
solução!

Olá Renan, veja só isso...

"org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'consultaController': Unsatisfied dependency expressed through field 'agenda': Error creating bean with name 'agendaDeConsultas': Unsatisfied dependency expressed through field 'consultaRepository': Error creating bean with name 'consultaRepository' defined in med.voll.api.domain.consulta.ConsultaRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Could not create query for public abstract boolean med.voll.api.domain.consulta.ConsultaRepository.existsByMedicoIdData(java.lang.Long,java.time.LocalDateTime);"

Vamos pegar agora a última linha desse trecho:

"Could not create query for public abstract boolean med.voll.api.domain.consulta.ConsultaRepository.existsByMedicoIdData(java.lang.Long,java.time.LocalDateTime);""

Ali tá dizendo algo como "Não foi possível criar uma query para o método existsByMedicoIdData(Long, LocalDateTime)". Mas afinal, o que tem de errado? Muito simples... Se você quer fazer uma consulta que verifica SE na tabela Medicos existe um Medico com a ID E a DATA passada no parâmetro, você precisa usar um AND para especificar qual é outro elemento que você quer verificar. A formatação correta do nome do método é muito importante, ou simplesmente não vai funcionar...

//errado
existsByMedicoIdData(Long data, LocalDateTime data);

//correto
existsByMedicoIdAndData()

//Vamos traduzir isso para: 
//"Existe uma consulta ONDE medico_id tenha um ID igual a esse aqui (?) E que tenha a data igual e a essa aqui(?)"

Boa tarde, Mateus.

Valeu pela ajuda, estava com esse problema há algum tempo, estou voltando a mexer no projeto agora. Apliquei a solução que você mecionou porem ocorreu outro erro no bean validation, esse me deixou confuso, pois não tenho ideia no que pode ser:

Error creating bean with name 'agendaDeConsultas': Unsatisfied dependency expressed through field 'medicoRepository': Error creating bean with name 'medicoRepository' defined in med.voll.api.domain.medico.MedicoRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Could not create query for public abstract java.lang.Boolean med.voll.api.domain.medico.MedicoRepository.findAtivoById(java.lang.Long); Reason: Validation failed for query for method public abstract java.lang.Boolean med.voll.api.domain.medico.MedicoRepository.findAtivoById(java.lang.Long)