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

Exception ao carregar o projeto

Olá, estou com problemas a seguir, mas não consigo resolver...

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 med.voll.api.domain.medico.Medico med.voll.api.domain.medico.MedicoRepository.escolherMedicoAleatorioDisponivel(med.voll.api.domain.medico.Especialidade,java.time.LocalDateTime); Reason: Validation failed for query for method public abstract med.voll.api.domain.medico.Medico med.voll.api.domain.medico.MedicoRepository.escolherMedicoAleatorioDisponivel(med.voll.api.domain.medico.Especialidade,java.time.LocalDateTime)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:714) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:694) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:483) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) ~[spring-context-6.0.10.jar:6.0.10]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.10.jar:6.0.10]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.1.jar:3.1.1]
    at med.voll.api.ApiApplication.main(ApiApplication.java:10) ~[classes/:na]
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.1.1.jar:3.1.1]

Entendo que está com problema no BeanValidation e no método escolherMedicoAleatorioDisponivel

8 respostas

Mas ele também acusa o seguinte erro no @Query

Caused by: java.lang.IllegalArgumentException: org.hibernate.query.sqm.InterpretationException: Error interpreting query [    select m from Medico m
    where
    m.ativo = 1
    and
    m.especialidade = :especialidade
    and
    m.id not in(
        select c.medico.id from Consulta c
        where
        c.data = :data
        and
        c.motivo_cancelamento is null
    )
    order by rand()
    limit 1
]; this may indicate a semantic (user query) problem or a bug in the parser [    select m from Medico m
    where
    m.ativo = 1
    and
    m.especialidade = :especialidade
    and
    m.id not in(
        select c.medico.id from Consulta c
        where
        c.data = :data
        and
        c.motivo_cancelamento is null
    )
    order by rand()
    limit 1
]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:168) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:795) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:704) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:120) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:360) ~[spring-orm-6.0.10.jar:6.0.10]
    at jdk.proxy4/jdk.proxy4.$Proxy123.createQuery(Unknown Source) ~[na:na]
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:94) ~[spring-data-jpa-3.1.1.jar:3.1.1]
    ... 65 common frames omitted
Caused by: org.hibernate.query.sqm.InterpretationException: Error interpreting query [    select m from Medico m
    where
    m.ativo = 1
    and
    m.especialidade = :especialidade
    and
    m.id not in(
        select c.medico.id from Consulta c
        where
        c.data = :data
        and
        c.motivo_cancelamento is null
    )
    order by rand()
    limit 1
]; this may indicate a semantic (user query) problem or a bug in the parser [    select m from Medico m
    where
    m.ativo = 1
    and
    m.especialidade = :especialidade
    and
    m.id not in(
        select c.medico.id from Consulta c
        where
        c.data = :data
        and
        c.motivo_cancelamento is null
    )
    order by rand()
    limit 1
]
    at org.hibernate.query.hql.internal.StandardHqlTranslator.translate(StandardHqlTranslator.java:97) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.lambda$interpretHql$2(AbstractSharedSessionContract.java:744) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.createHqlInterpretation(QueryInterpretationCacheStandardImpl.java:141) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.resolveHqlInterpretation(QueryInterpretationCacheStandardImpl.java:128) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.interpretHql(AbstractSharedSessionContract.java:741) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:786) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    ... 72 common frames omitted
Caused by: java.lang.IllegalArgumentException: Can't compare test expression of type [BasicSqmPathSource(ativo : Boolean)] with element of type [basicType@15(java.lang.Integer,4)]
    at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.assertComparable(SqmCriteriaNodeBuilder.java:2098) ~[hibernate-core-
    
    ................

Qual seria o erro no select usado? Não consigo entender...

Ah, tudo isso é na classe MedicoRepository.

Classe Médico

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

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private Boolean ativo;

    private String nome;
    private String email;
    private String telefone;
    private String crm;

    @Enumerated(EnumType.STRING)
    private Especialidade especialidade;

    @Embedded
    private Endereco endereco;

Oi!

Altere nessa query de m.ativo = 1 para: m.ativo = true e veja se resolve.

Continua dando exception...

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:714) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:694) ~[spring-beans-6.0.10.jar:6.0.10]
    
    ......................
    
    aused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 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:714) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:694) ~[spring-beans-6.0.10.jar:6.0.10]
    ..........................
    
    Caused by: org.springframework.data.repository.query.QueryCreationException: 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.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-3.1.1.jar:3.1.1]

    .............................
    
    Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.lang.Boolean med.voll.api.domain.medico.MedicoRepository.findAtivoById(java.lang.Long)
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:100) ~[spring-data-jpa-3.1.1.jar:3.1.1]
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:70) ~[spring-data-jpa-3.1.1.jar:3.1.1]
    
    
    ..............................
    
    Caused by: java.lang.IllegalArgumentException: org.hibernate.query.sqm.ParsingException: line 2:11 mismatched input ':' expecting {<EOF>, '.', '[', '+', '-', '*', '/', '%', '||', AND, BY, DAY, EPOCH, GROUP, HOUR, MINUTE, MONTH, NANOSECOND, OR, ORDER, QUARTER, SECOND, WEEK, YEAR}
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
    
    

Estes foram todos os erros que apareceram.

Classe MedicoRepository:

package med.voll.api.domain.medico;

imports...

public interface MedicoRepository extends JpaRepository<Medico, Long> {

    Page<Medico> findAllByAtivoTrue(Pageable pageable);

    @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 escolherMedicoAleatorioDisponivel(Especialidade especialidade, LocalDateTime data);

    @Query("""
            select m.ativo from Medico m
            where m.id :id
            """)
    Boolean findAtivoById(Long id);
}
solução!

Agora o erro é na query no método findAtivoById:

select m.ativo from Medico m where m.id :id

Faltou um sinal de igual antes do :id

select m.ativo from Medico m where m.id = :id

Achei o erro, faltava um sinal de igual.

KKKKKKK, obrigado Rodrigo pelo apoio. Só queria tirar uma dúvida... Porque precisamos usar m.atito = true e não podemos usar m.ativo = 1. Java não realiza a conversão implícita de 1 para true e 0 para false? Ou é o Hibernate mesmo que não faz isso?

Legal!

De vez em quando tem esse mesmo problema sendo reportado no fórum. Acredito que com a nova versão do Spring Boot, que atualizou o Hibernate, deve ter perdido essa conversão automática. Na versão utilizada na gravação funciona tranquilo, mas nas novas versões parece ter esse problema.

Bons estudos!