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

[Dúvida] Error starting ApplicationContext.

Após implementar as validações na aplicação, o programa está me retornando o seguinte erro:

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2024-07-04T14:03:06.424-03:00 ERROR 20404 --- [ restartedMain] o.s.boot.SpringApplication : 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.existsPacienteIdAndDataBetween(java.lang.Long,java.time.LocalDateTime,java.time.LocalDateTime); Reason: Failed to create query for method public abstract boolean med.voll.api.domain.consulta.ConsultaRepository.existsPacienteIdAndDataBetween(java.lang.Long,java.time.LocalDateTime,java.time.LocalDateTime); No property 'existsPacienteId' found for type 'Consulta' at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:712) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:692) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:127) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:481) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1397) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:961) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:915) ~[spring-context-6.0.2.jar:6.0.2] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[spring-context-6.0.2.jar:6.0.2] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.0.0.jar:3.0.0] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-3.0.0.jar:3.0.0] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) ~[spring-boot-3.0.0.jar:3.0.0] at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-3.0.0.jar:3.0.0] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[spring-boot-3.0.0.jar:3.0.0] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[spring-boot-3.0.0.jar:3.0.0] 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:49) ~[spring-boot-devtools-3.0.0.jar:3.0.0]...

6 respostas

Oi!

Na sua interface ConsultaRepository tem esse método:

existsPacienteIdAndDataBetween

Faltou o By no nome do método. Correto:

existsByPacienteIdAndDataBetween

Mudou o erro agora:

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2024-07-07T19:10:21.644-03:00 ERROR 11348 --- [ restartedMain] o.s.boot.SpringApplication : 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 '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) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:712) ~[spring-beans-6.0.2.jar:6.0.2]...

Agora o erro é na interface MedicoRepository. Manda aqui o código dela e também da classe Medico

Eu tinha tentado usar a Luri pra procurar o erro, mas ela fica me apontando coisas do código que ja estão da forma que ela está indicando.

classe medico:

package med.voll.api.domain.medico;

import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import med.voll.api.domain.endereco.Endereco;

@Table(name = "medicos") @Entity(name = "Medico") @Getter @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(of = "id") public class Medico {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome;
private String email;

private String telefone;

private String crm;

@Enumerated(EnumType.STRING)
private Especialidade especialidade;

@Embedded
private Endereco endereco;

private Boolean ativo;

public Medico(DadosCadastroMedico dados) {
    this.ativo = true;
    this.nome = dados.nome();
    this.email = dados.email();
    this.telefone = dados.telefone();
    this.crm = dados.crm();
    this.especialidade = dados.especialidade();
    this.endereco = new Endereco(dados.endereco());
}

public void atualizarInformacoes(DadosAtualizacaoMedico dados) {
    if (dados.nome() != null) {
        this.nome = dados.nome();
    }
    if (dados.telefone() != null) {
        this.telefone = dados.telefone();
    }
    if (dados.endereco() != null) {
        this.endereco.atualizarInformacoes(dados.endereco());
    }

}

public void excluir() {
    this.ativo = false;
}

}

classe medico repository:

package med.voll.api.domain.medico;

import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query;

import java.time.LocalDateTime;

public interface MedicoRepository extends JpaRepository<Medico, Long> { Page findAllByAtivoTrue(Pageable paginacao);

@Query("""
        select m from Medico m
        where
        m.ativo = true
        and
        m.especialidade = :especialidade
        and
        m.id not in(
            select c.medico.id from Consulta c
            where
            c.data = :data
            )
        order by rand()
        limit 1
        
        """)
Medico escolherMedicoAleatorioLivreNaData(Especialidade especialidade, LocalDateTime data);

@Query("""
        select m.ativo
        from medico m
        where
        m.id = :id
        """)

Boolean findAtivoById(Long id);

}

solução!

O problema está na query do seu método:

@Query("""
        select m.ativo
        from medico m
        where
        m.id = :id
        """)
Boolean findAtivoById(Long id);

Está from medico (medico com m em minúsculo)

Correção:

@Query("""
        select m.ativo
        from Medico m
        where
        m.id = :id
        """)
Boolean findAtivoById(Long id);

Agora rodou, muito obrigado