2
respostas

Erro JPA

Boa noite! Estou recebendo um erro recorrente referente a consulta na entidade Pacientes. Tentei de tudo, mas não consigo achar o problema...

Erro: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appoimentController': Unsatisfied dependency expressed through field 'appoimentSchedule': Error creating bean with name 'appoimentSchedule': Unsatisfied dependency expressed through field 'appoimentRepository': Error creating bean with name 'appoimentRepository' defined in med.voll.api.domain.consultas.AppoimentRepository defined in @EnableJpaRepositories declared on PersistenceJPAConfig: Could not create query for public abstract boolean med.voll.api.domain.consultas.AppoimentRepository.existsByPacientAndDataBetween(java.lang.Long,java.time.LocalDateTime,java.time.LocalDateTime); Reason: Failed to create query for method public abstract boolean med.voll.api.domain.consultas.AppoimentRepository.existsByPacientAndDataBetween(java.lang.Long,java.time.LocalDateTime,java.time.LocalDateTime); Can't compare test expression of type [Pacient] with element of type [basicType@5(java.lang.Long,-5)] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:713) ~[spring-beans-6.0.9.jar:6.0.9]

PacientRepository.class

package med.voll.api.domain.pacientes;

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 org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface PacientRepository extends JpaRepository<Pacient, Long> {
    Page<Pacient> findAllByAtivoTrue(Pageable page);

    @Query("""
            select
            p.ativo
            from Pacient p 
            where
            p.id = :id
            """)

    boolean findActiveById(
            Long id
    );
}

Pacient.class

@Table(name = "pacientes")
@Entity
@NoArgsConstructor
@Data
public class Pacient {

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

    private String nome;
    private String email;
    private String cpf;
    private String telefone;

    @Embedded
    private Endereco endereco;

    private Boolean ativo;

    public Pacient(PatientRegistrationData data){
        this.ativo = true;
        this.nome = data.nome();
        this.email = data.email();
        this.cpf = data.cpf();
        this.telefone = data.telefone();
        this.endereco = new Endereco(data.endereco());
    }

    public void updateInfo(PacientUpdateData data) {
        if (data.nome() != null)
            this.nome = data.nome();

        if (data.telefone() != null)
            this.telefone = data.telefone();

        if (data.endereco() != null)
            endereco.uptadeInfo(data.endereco());
    }

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

}

PacientActiveValidation:

public class PacientActiveValidation {
    private PacientRepository repository;
    public void validation(AppoimentData data) {
        var pacientIsActive = repository.findActiveById(data.pacientId());

        if (!pacientIsActive){
            throw new ValidacaoException("Consulta não pode ser agendada com paciente excluído");

        }
    }
}
2 respostas

Oi!

O problema no log: Reason: Failed to create query for method public abstract boolean med.voll.api.domain.consultas.AppoimentRepository.existsByPacientAndDataBetween(java.lang.Long,java.time.LocalDateTime,java.time.LocalDateTime); Can't compare test expression of type [Pacient] with element of type [basicType@5(java.lang.Long,-5)]

Ou seja, o problema está no AppoimentRepository, no método existsByPacientAndDataBetween

O nome desse método deveria ser: existsByPacientIdAndDataBetween