Home > Software engineering >  how to fix: Unsatisfied dependency expressed through method 'returnReasonRepository' param
how to fix: Unsatisfied dependency expressed through method 'returnReasonRepository' param

Time:06-22

I am trying to run the spring boot application but I got the below error:

Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'returnReasonRepository' defined in class path resource [muscat/configuration/RepositoryConfiguration.class]: Unsatisfied dependency expressed through method 'returnReasonRepository' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springReturnReasonRepository' defined in muscat.repository.SpringReturnReasonRepository defined in @EnableJpaRepositories declared on ServiceApplication: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.Optional .muscat.repository.SpringReturnReasonRepository.findByCode(java.lang.String)! Reason: Failed to create query for method public abstract java.util.Optional .muscat.repository.SpringReturnReasonRepository.findByCode(java.lang.String)! No property code found for type ReturnReason!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional .muscat.repository.SpringReturnReasonRepository.findByCode(java.lang.String)! No property code found for type ReturnReason!

this is the entity:

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import static javax.persistence.GenerationType.IDENTITY;

    @Entity
    @Data
    @EqualsAndHashCode(of = "id", callSuper = false)
    public class ReturnReason {
        @Id
        @GeneratedValue(strategy = IDENTITY)
        private Long id;
    
        @Column
        private String reasonCode;
    
        @Column
        private String reasonDescription;
    
    
        @Column
        private String reasonType;
    }


and this is the 

        public interface ReturnReasonRepository extends MessageRepository<ReturnReason> {
            Optional<ReturnReason> findByCode(String reasonCode);
        }
        
        
        public class ReturnReasonRepositoryImpl implements ReturnReasonRepository {
            private final SpringReturnReasonRepository springReturnReasonRepository;
        
            public ReturnReasonRepositoryImpl(SpringReturnReasonRepository springReturnReasonRepository) {
                this.springReturnReasonRepository = springReturnReasonRepository;
            }
        
        
            @Override
            public Optional<ReturnReason> findByCode(String reasonCode) {
                return springReturnReasonRepository.findByCode(reasonCode);
            }
        
            @Override
            public ReturnReason save(ReturnReason returnReason) {
                return springReturnReasonRepository.save(returnReason);
            }
    
    
        public interface SpringReturnReasonRepository extends CrudRepository<ReturnReason, Long>, JpaSpecificationExecutor<ReturnReason> {
            Optional<ReturnReason> findByCode(String reasonCode);
        }
    
    
    @Configuration
    public class RepositoryConfiguration {
    
    
        @Bean
        public BankInformationRepository bankInformationRepository(SpringBankInformationRepository springBankInformationRepository) {
            return new BankInformationRepositoryImpl(springBankInformationRepository);
        }
    
        @Bean
        public ReturnReasonRepository returnReasonRepository(SpringReturnReasonRepository springReturnReasonRepository) {
            return new ReturnReasonRepositoryImpl(springReturnReasonRepository);
        }
    }

what is my mistake and how can I solve it?

CodePudding user response:

muscat.repository.SpringReturnReasonRepository.findByCode(java.lang.String)! No property code found for type ReturnReason!

This is your error. In SpringReturnReasonRepository, you have findByCode but there is no property named code in your ReturnReason entity. If you rename this method to findByReasonCode, it will work.

If you want to know more about JPA repository method naming convention, you can refer this link.

  • Related