Search code examples
javaspring-bootspring-data-cassandra

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException


I am working on a spring boot application. After trying many solutions, it's not getting resolved.Please help:

Console: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'doctorController': Unsatisfied dependency expressed through field 'doctorService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'doctorService': Unsatisfied dependency expressed through field 'doctorRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'doctorRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property id found for type Doctor!

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'doctorService': Unsatisfied dependency expressed through field 'doctorRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'doctorRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property id found for type Doctor!

Controller:

@RestController
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @RequestMapping("/hms/doctor")
    public List<Doctor> getAllDoctor()
    {
        return doctorService.getAllDoctor();
    }

    @RequestMapping(method=RequestMethod.POST,value="hms/doctor")
    public void addDoctor(@RequestBody Doctor doctor)
    {
        doctorService.addDoctor(doctor);

    }
}

Service class:

@Service
public class DoctorService {

    Logger logger= LoggerFactory.getLogger(DoctorService.class);

    @Autowired
    private DoctorRepository doctorRepo;

    public List<Doctor> getAllDoctor(){

        logger.error("error happened");
        logger.trace(" Error !!!");
        List<Doctor> doctor= new ArrayList<Doctor>();
        doctorRepo.findAll().forEach(doctor::add);      
        return doctor;

    }

    public Doctor getDoctorById(String doctorId) {
        return doctorRepo.findById(doctorId);       
    }

    public Doctor getDoctorByPhoneNumber(long phoneNumber)
    {
        return doctorRepo.findByPhoneNumber(phoneNumber);

    }


    public void addDoctor(Doctor doctor) {
        doctorRepo.save(doctor);                
    }

    public void updateDoctor(String doctorId,Doctor doctor) {
        doctorRepo.save(doctor);
    }
}

Repository:

public interface DoctorRepository extends CassandraRepository<Doctor, Integer>
{

    Doctor findById(String doctorId);

    Doctor findByPhoneNumber(long phoneNumber);

    void deleteById(String doctorId);

}

Model class:

@Entity
public class Doctor {

    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    @PrimaryKey
    private int doctorId;
    private String doctorName;
    private long phoneNumber;
    private String specialization;
    private int totalExperience;
    private String workingDays[]= {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};

    public Doctor() {

    }

    public Doctor(int doctorId, String doctorName, long phoneNumber, String specialization, int totalExperience,
            String[] workingDays) {`enter code here`
        super();
        this.doctorId = doctorId;
        this.doctorName = doctorName;
        this.phoneNumber = phoneNumber;
        this.specialization = specialization;
        this.totalExperience = totalExperience;
        this.workingDays = workingDays;
// getters and setters 

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.fullstack.assessment</groupId>
    <artifactId>hms</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hms</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-cassandra</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

<!--    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency> -->   
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Solution

  • The Id of Doctor is doctorId, so

     Doctor findById(...
     void deleteById(...
    

    should be

    Doctor findByDoctorId(...
    void deleteByDoctorId(...
    

    or change doctorId to simply id, with getter and setter.